The following two lines were run in the current Bash, and the result was <>.
Without changing LAB_COLOR, make the second command print <blue> when it is run again.
LAB_COLOR=blue
bash -c 'printf "<%s>\n" "$LAB_COLOR"'Which command should be added between the two lines?
Shell variables are not inherited by child processes automatically. Exporting the variable places it in the environment inherited by a Bash started later.
Detailed explanation
export LAB_COLORCorrect. export marks the existing shell variable for inclusion in the environment of child processes.
Correct. export marks the existing shell variable for inclusion in the environment of child processes.
set LAB_COLORIncorrect. This sets positional parameters; it does not give LAB_COLOR the export attribute.
Incorrect. This sets positional parameters; it does not give LAB_COLOR the export attribute.
echo "$LAB_COLOR"Incorrect. echo only prints the value in the current shell and does not change the child environment.
Incorrect. echo only prints the value in the current shell and does not change the child environment.
unset LAB_COLORIncorrect. unset removes the variable, so neither the current shell nor a child shell can read it.
Incorrect. unset removes the variable, so neither the current shell nor a child shell can read it.
Try it yourself
An example you can run in a temporary verification environment.
unset LAB_COLOR
LAB_COLOR=blue
bash -c 'printf "before=<%s>\n" "$LAB_COLOR"'
export LAB_COLOR
bash -c 'printf "after=<%s>\n" "$LAB_COLOR"'Expected result
before=<>
after=<blue>Key points
- The difference between shell and environment variables
- Inheritance by child processes
- When expansion occurs inside single quotes
Notes
- Environment:
- 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.