The current Bash has LAB_MODE=dev.
Keep the current value unchanged, but make only the next child Bash print LAB_MODE=prod.
Which command should be used?
A variable assignment placed before a simple command creates a temporary environment for that command. The parent shell variable remains unchanged.
Detailed explanation
LAB_MODE=prod bash -c 'printf "%s\n" "$LAB_MODE"'Correct. The assignment is passed only to this child Bash, while the current shell keeps LAB_MODE=dev.
Correct. The assignment is passed only to this child Bash, while the current shell keeps LAB_MODE=dev.
export LAB_MODE=prod; bash -c 'printf "%s\n" "$LAB_MODE"'Incorrect. export LAB_MODE=prod changes the current shell's value as well.
Incorrect. export LAB_MODE=prod changes the current shell's value as well.
LAB_MODE=prod; bash -c 'printf "%s\n" "$LAB_MODE"'Incorrect. This assignment alone changes the current shell, and the following command is separate.
Incorrect. This assignment alone changes the current shell, and the following command is separate.
bash -c 'printf "%s\n" "$LAB_MODE"' LAB_MODE=prodIncorrect. The word after the command is argv[0], not an environment assignment.
Incorrect. The word after the command is argv[0], not an environment assignment.
Try it yourself
An example you can run in a temporary verification environment.
LAB_MODE=dev
LAB_MODE=prod bash -c 'printf "child=%s
" "$LAB_MODE"'
printf 'parent=%s
' "$LAB_MODE"Expected result
child=prod
parent=devKey points
- Command-prefix assignments
- The child process environment
- Keeping the parent value
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.