LAB_FLAG=on is defined in the current Bash.
Make the variable unset rather than assigning it an empty string.
Which command is appropriate?
unset removes a variable definition. That differs from assigning an empty value, which leaves the variable defined.
Detailed explanation
LAB_FLAG=Incorrect. LAB_FLAG= leaves the variable defined with an empty value.
An empty assignment leaves the variable set. The value can print as empty while [[ -v LAB_FLAG ]] remains true, so printing the value alone does not establish removal.
unset LAB_FLAGCorrect. unset removes the variable definition from the current shell.
For the ordinary mutable scalar in this exercise, removing its definition makes [[ -v LAB_FLAG ]] false. This tests the state required by the question rather than merely looking at an empty display.
export LAB_FLAGIncorrect. export changes inheritance attributes but does not remove the variable.
The existing on value remains and the variable stays set after the export attribute is added. Inheritance and removal are separate changes.
set LAB_FLAGIncorrect. With LAB_FLAG as an argument, set changes positional parameters rather than removing the named variable.
With LAB_FLAG as an argument, set assigns the literal text LAB_FLAG to $1. It leaves the named variable LAB_FLAG=on intact, so the positional parameter and the named variable must be checked separately.
Try it yourself
An example you can run in a temporary verification environment.
LAB_FLAG=
if [[ -v LAB_FLAG ]]; then printf 'empty=set\n'; else printf 'empty=unset\n'; fi
printf 'empty/default=<%s>\n' "${LAB_FLAG-fallback}"
printf 'empty/colon=<%s>\n' "${LAB_FLAG:-fallback}"
unset LAB_FLAG
if [[ -v LAB_FLAG ]]; then printf 'removed=set\n'; else printf 'removed=unset\n'; fi
printf 'removed/default=<%s>\n' "${LAB_FLAG-fallback}"Official references
- Official documentation 1
- Official documentation 2
- Official documentation 3
- Official documentation 4
- Official documentation 5
Expected result
empty=set
empty/default=<>
empty/colon=<fallback>
removed=unset
removed/default=<fallback>Key points
- Unset versus an empty string
- The role of unset
- Checking definition with [[ -v ]]
Notes
- Environment: Bash 5.2. Use an ordinary mutable scalar variable in a fresh Bash process.
- Use an ordinary mutable scalar in a fresh Bash process. Readonly variables and other variable types have additional rules.
- Compare empty=set with removed=unset in the supplied verification. The default-expansion lines are supporting observations of the same empty-versus-unset distinction.
Foundation review
Test existence separately from display
After an empty assignment, the -v test is true; after removal it is false. Both values can look empty when printed, making an explicit state test useful.
Default expansion with and without a colon
${LAB_FLAG-fallback} uses the fallback only when the variable is unset. ${LAB_FLAG:-fallback} also uses it for an empty value. The colon changes which condition is tested.
Identify the state being changed
Assignment changes a value, export changes inheritance, set with arguments changes positional parameters, and unset removes a definition. The required state here belongs to the current named scalar variable.