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.
The prefix supplies prod to the external bash command while the parent retains dev. Check both observations: the child output and the parent value afterward. This temporary value does not require changing the parent's stored value.
export LAB_MODE=prod; bash -c 'printf "%s\n" "$LAB_MODE"'Incorrect. export LAB_MODE=prod changes the current shell's value as well.
The export command runs in the parent and assigns prod there as well. A matching child output alone is insufficient when the parent must retain dev.
LAB_MODE=prod; bash -c 'printf "%s\n" "$LAB_MODE"'Incorrect. This assignment alone changes the current shell, and the following command is separate.
The semicolon makes the assignment a completed command in the parent, changing its value to prod. Whether the child inherits that value depends on the existing export attribute, but the parent-value requirement already fails.
bash -c 'printf "%s\n" "$LAB_MODE"' LAB_MODE=prodIncorrect. The first word after the bash -c command string sets the child shell's $0; it is not an environment assignment.
For bash -c, the first argument after the command string sets the child shell's $0. With the stated standard settings, the trailing word is not an environment assignment. An unexported parent value is absent in the child, while an exported parent value remains dev.
Try it yourself
An example you can run in a temporary verification environment.
LAB_MODE=dev
LAB_MODE=prod bash -c 'printf "child=%s\n" "$LAB_MODE"'
printf 'parent=%s\n' "$LAB_MODE"Official references
Expected result
child=prod
parent=devKey points
- Command-prefix assignments
- The child process environment
- Keeping the parent value
Notes
- Environment: Bash 5.2 in a fresh process with an ordinary mutable scalar variable. Automatic-export (-a) and keyword-assignment (-k) modes are off.
- Run the supplied verification in a fresh Bash process and compare child=prod with parent=dev. The working shell outside that process is unaffected.
- The exercise assumes the external bash command with standard options. Check both exported and unexported starting states rather than asserting the same child output for every wrong option.
Foundation review
Observe parent and child separately
The child printing prod and the parent retaining dev are two distinct requirements. A change that satisfies only the child output is insufficient.
Do not assume the export state
A standalone assignment is not automatically inherited from an unexported variable. An already exported variable can retain that attribute, so the child output can differ even when the parent-value violation is the same.
Argument placement matters
An assignment word before the command name and an argument after the bash -c string have different roles. The first argument following that string sets $0 rather than $1.