Variables and environment basics
On this page
A value prints correctly at your prompt, but a command started in another Bash sees an empty value. Repeating the assignment in the first shell does not explain the difference. You need to know which values stay in one shell and which travel to a child.
By the end, you can store text, preserve it as one argument, and export selected values without confusing a child’s changes with its parent’s state.
Before you start
Section titled “Before you start”Finish Quotes and escaping (B08.2). Use only ~/lab/b08/variables, with two empty directories for the OLDPWD example:
mkdir -p ~/lab/b08/variables/first ~/lab/b08/variables/secondcd ~/lab/b08/variablesKeep earlier lab exercises. Use Reset this lesson before repeating this setup.
Store text without running a command
Section titled “Store text without running a command”name='course notes'A variable associates a name with a value. This assignment stores the two words as one value. It prints nothing. There are no spaces around =: adding them would make different shell words rather than this assignment.
For ordinary variable names, use letters, digits, and underscores, with no digit first. Names are case-sensitive: name and NAME are different variables. Keep these practice names as written so every later command refers to the value you assigned.
Expand the value inside double quotes:
printf '%s\n' "$name"course notesThe dollar sign asks Bash for the value. The double quotes preserve that value as one argument. The assignment quotes themselves were not stored.
Predict first: will removing the double quotes still give printf one argument containing a space?
printf '%s\n' $nameRun it, then compare with your guess
printf '%s\n' $namecoursenotesNo. Under the default splitting rules used here, the unquoted expansion becomes two arguments. printf repeats its format for each one. Unquoted expansions can also expose wildcard characters to filename expansion; B08.5 teaches that next step. Quote values when they should remain one argument.
learner@rootvolt:~/lab/b08/variables$ name='course notes'learner@rootvolt:~/lab/b08/variables$ printf '%s\n' "$name"course noteslearner@rootvolt:~/lab/b08/variables$ printf '%s\n' $namecourse
noteslearner@rootvolt:~/lab/b08/variables$ printf '[%s]\n' "${name}_ready"[course notes_ready]learner@rootvolt:~/lab/b08/variables$ Steps (11)
- Type
name='course notes' - Press Enter
- Type
printf '%s\n' "$name" - Press Enter
- Output:
course notes - Type
printf '%s\n' $name - Press Enter
- Output:
course notes - Type
printf '[%s]\n' "${name}_ready" - Press Enter
- Output:
[course notes_ready]
The replay also shows braces delimiting the variable name before an underscore:
printf '[%s]\n' "${name}_ready"[course notes_ready]${name} ends the variable reference before _ready. Without those braces, $name_ready would ask for a different variable. These braces identify a variable name; B08.5’s brace expansion is a different operation.
| What you type | Arguments after the format |
|---|---|
"$name" |
One: course notes |
$name |
Two: course, notes |
"${name}_ready" |
One: course notes_ready |
Start a child with only selected values
Section titled “Start a child with only selected values”Prepare two shell-local variables:
local_note=privateshared_note=readyA child process is a process started by another process. bash -c starts a child Bash that reads the quoted command string, as in B08.1. The outer single quotes keep the parent from expanding the variable references: the child reads them instead.
bash -c 'printf "before: local=[%s] shared=[%s]\n" "$local_note" "$shared_note"'before: local=[] shared=[]Both bracket pairs are empty. The parent has values, but these ordinary assignments did not put them in the child’s environment, the selected name/value entries passed to a started process. Empty expansion alone cannot distinguish an unset variable from a variable set to an empty string.
Choose one value to pass:
export shared_noteexport marks that variable for inclusion in the environment of subsequently started commands. Inspect only that entry:
printenv shared_notereadyprintenv inspects its own inherited environment. Giving it one name keeps the output focused; do not print your entire environment to compare with this example.
Predict first: will the next child receive both values, one, or neither?
bash -c 'printf "after: local=[%s] shared=[%s]\n" "$local_note" "$shared_note"'Run it, then compare with your guess
bash -c 'printf "after: local=[%s] shared=[%s]\n" "$local_note" "$shared_note"'after: local=[] shared=[ready]Only shared_note crosses. Export did not select local_note, and it did not retroactively update any already running child.
In words: the parent stores local_note=private and shared_note=ready. After export shared_note, the child inherits shared_note=ready, while local_note is absent. This model omits the other environment entries inherited from the terminal session; it does not claim that the child has only one entry in total.
A child does not rewrite its parent
Section titled “A child does not rewrite its parent”The next command string contains two commands separated by ;. The child executes them in order: assign its own value, then print it.
bash -c 'shared_note=child; printf "%s\n" "$shared_note"'childCheck the parent at your prompt:
echo "$shared_note"readyThe parent still has ready. Inheritance copies values into the child’s starting environment; it is not a shared variable that writes back to the parent.
Connect OLDPWD to cd -
Section titled “Connect OLDPWD to cd -”B05.3 used cd - and mentioned OLDPWD. Set two known locations:
cd firstcd ../secondecho "$OLDPWD"/home/learner/lab/b08/variables/firstOLDPWD holds the previous working directory, maintained by Bash when you change directories. cd - uses it:
cd -/home/learner/lab/b08/variables/firstThe command returns to first and prints its path. Return to the lesson directory before the challenge:
cd ~/lab/b08/variablesMistakes to recognize
Section titled “Mistakes to recognize”Putting spaces around =. name='course notes' is an assignment. Separating name from = does not mean the same thing.
Expanding the parent’s value too early. Double quotes around a bash -c string would allow the parent to expand its references. Use the single-quoted strings shown here to test what the child sees.
Assuming export sends later changes into existing processes. A child receives its starting environment when launched. Start another child to test an updated value.
Try it yourself
Section titled “Try it yourself”Goal: Pass the phrase review complete to a child as one argument, let that child change its own value, and prove that the parent retains the phrase.
Use the variable handoff rather than changing the worked example’s two variables.
Hint
Assign, export, then start a child using a single-quoted command string. Quote the expansion inside that string too.
Solution
handoff='review complete'export handoffbash -c 'printf "[%s]\n" "$handoff"'[review complete]bash -c 'handoff=checked; printf "%s\n" "$handoff"'checkedprintf '%s\n' "$handoff"review completeThe child’s checked did not replace the parent’s review complete.
Reset this lesson
Section titled “Reset this lesson”Remove only the practice variables from this shell:
unset name local_note shared_note handoffunset removes these named variables. Do not unset HOME, PATH, or unrelated values. Then inspect and remove this lesson’s disposable directories:
cd ~/lab/b08ls -AR variablesrm -r ~/lab/b08/variablesRerun Before you start. If the directory is absent, skip removal. The two subdirectories are recreated empty.
- An assignment stores a value; a quoted expansion keeps that value as one argument.
exportselects variables for subsequently started processes;printenv NAMEinspects a specific inherited entry.- A child can change its own values without rewriting the parent’s variables.
- Bash maintains
OLDPWD, which supplies the destination forcd -.
Next, PATH and command resolution follows an environment value Bash uses to locate programs.
Further reading
Section titled “Further reading”- On your system:
help export,help unset,help cd,man printenv, andman bash. - GNU Bash: Shell Parameters, Shell Parameter Expansion, and Environment.