Shell variables and environment
Quick answer
A shell variable stores a value in the current shell. Export it when a child program needs that value, for example a report directory or locale.
Prepare the practice environment
Run python3 lab-fixtures/shell-scripting/setup.py as an ordinary user from the extracted package root in a disposable Linux environment. Enter the printed directory, check pwd, then run export LC_ALL=C; umask 077; set -o pipefail. Use a fresh fixture for each example. Save the code as example.sh inside that directory and run bash --noprofile --norc -o pipefail example.sh rather than sourcing it. Stop if a command fails unexpectedly. All output paths must be new. The source of check-report.sh is in the fixture JSON. Outputs below are not commands.
How it works
Assign with name=value (no spaces around =). Expand with "$name". export name marks a variable for inheritance by subsequently executed programs. A child gets its own environment; changing it does not update the parent. NAME=value command can supply an environment value for a single external command. Keep secrets out of examples, command arguments and tracing. bash script.sh runs a file using Bash, regardless of its shebang; direct execution also needs appropriate permissions and an interpreter declaration.
A shell variable can exist with an empty value; this differs from an unset variable. In ${BM_REPORT-unset}, the fallback is used only when the variable is unset, not when it is set to an empty string. In the example with BM_MODE=preview bash ..., only the new Bash process receives the temporary value; the parent keeps normal. This is useful for testing one invocation without changing the environment for the next command.
Try the examples
The assignment itself prints nothing; printf displays the value.
Expected stdout:
notes.txtunset BM_REPORTBM_REPORT=notes.txtbash -c 'printf "%s\n" "${BM_REPORT-unset}"'export BM_REPORTbash -c 'printf "%s\n" "$BM_REPORT"'Single quotes defer expansion to the child shell.
Expected stdout:
unsetnotes.txtexport BM_MODE=normalBM_MODE=preview bash -c 'printf "%s\n" "$BM_MODE"'printf '%s\n' "$BM_MODE"The external command receives the temporary value; the parent keeps its own.
Expected stdout:
previewnormalCommon mistakes
An assignment such as report = notes.txt is parsed as a command, not an assignment. Do not export the entire environment or print secrets.
Practice
Create a variable BM_OUTPUT, show that a child cannot see it before export, then show that changing it inside the child leaves the parent unchanged.
Related references
Study: export, env, bash. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Shell variables and environment — Knowledge Check
Apply the lesson concepts to the controlled shell fixture.
Which assignment is valid?
What does export do?
Can a child change its parent variable directly?
Which value should not be printed in a debug log?