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.
LAB_COLOR already holds blue. Adding the export attribute keeps that value and makes it available in the environment of a Bash started afterward. It does not retroactively modify a child that is already running.
set LAB_COLORIncorrect. This sets positional parameters; it does not give LAB_COLOR the export attribute.
With LAB_COLOR as an argument and no option, set replaces positional parameters: $1 becomes the literal text LAB_COLOR. This does not add the export attribute to the named variable. In the stated shell, the value remains blue but the newly started child still lacks it.
echo "$LAB_COLOR"Incorrect. echo only prints the value in the current shell and does not change the child environment.
The current shell expands the value before echo prints it. Seeing blue in that output demonstrates visibility in the parent, but printing a value does not modify the environment passed to another process.
unset LAB_COLORIncorrect. unset removes the variable, so neither the current shell nor a child shell can read it.
Removing LAB_COLOR leaves no named value to pass to the next child. The later child therefore still prints an empty value instead of the required blue.
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"'Official references
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: Bash. Use a fresh Bash process so changes to variables and positional parameters do not affect your working shell.
- Run the supplied verification sequence in a fresh Bash process: it removes only the task variable there and should print before=<> followed by after=<blue>.
- The initial empty result is part of the premise. An assignment to a variable that was already exported can retain its export attribute; do not assume every assignment creates a non-exported variable.
Foundation review
Assignment and export are separate
In this scenario, the observed empty child output establishes that LAB_COLOR is not inherited. Assigning blue stores a shell value; adding the export attribute changes which named values later commands inherit.
Choose the expansion time
The single quotes around the bash -c program stop the outer shell from expanding $LAB_COLOR. The child Bash parses that program and expands the variable using its own environment.
Inheritance starts with the child
A child receives its environment when it starts. Subsequent changes in the child normally do not change the parent shell, and exporting in the parent affects commands started afterward.