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.
Incorrect. LAB_FLAG= leaves the variable defined with an empty value.
unset LAB_FLAGCorrect. unset removes the variable definition from the current shell.
Correct. unset removes the variable definition from the current shell.
export LAB_FLAGIncorrect. export changes inheritance attributes but does not remove the variable.
Incorrect. export changes inheritance attributes but does not remove the variable.
set LAB_FLAGIncorrect. set displays or changes shell attributes; it is not the removal command.
Incorrect. set displays or changes shell attributes; it is not the removal command.
Try it yourself
An example you can run in a temporary verification environment.
LAB_FLAG=on
unset LAB_FLAG
if [[ -v LAB_FLAG ]]; then echo set; else echo unset; fiExpected result
unsetKey points
- Unset versus an empty string
- The role of unset
- Checking definition with [[ -v ]]
Notes
- Environment: Bash 5.2
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.