Skip to content

Variables and environment basics

Module
How the Shell Interprets Input
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
Prerequisites
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.

Finish Quotes and escaping (B08.2). Use only ~/lab/b08/variables, with two empty directories for the OLDPWD example:

Terminal window
mkdir -p ~/lab/b08/variables/first ~/lab/b08/variables/second
cd ~/lab/b08/variables

Keep earlier lab exercises. Use Reset this lesson before repeating this setup.

Terminal window
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:

Terminal window
printf '%s\n' "$name"
Captured output
course notes

The 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?

Terminal window
printf '%s\n' $name
Run it, then compare with your guess
Terminal window
printf '%s\n' $name
Captured output
course
notes

No. 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.

Keep a value with spaces together
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The quoted name value stays on one line; its unquoted expansion supplies two arguments.
Steps (11)
  1. Type name='course notes'
  2. Press Enter
  3. Type printf '%s\n' "$name"
  4. Press Enter
  5. Output: course notes
  6. Type printf '%s\n' $name
  7. Press Enter
  8. Output: course notes
  9. Type printf '[%s]\n' "${name}_ready"
  10. Press Enter
  11. Output: [course notes_ready]

The replay also shows braces delimiting the variable name before an underscore:

Terminal window
printf '[%s]\n' "${name}_ready"
Captured output
[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

Prepare two shell-local variables:

Terminal window
local_note=private
shared_note=ready

A 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.

Terminal window
bash -c 'printf "before: local=[%s] shared=[%s]\n" "$local_note" "$shared_note"'
Captured output
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:

Terminal window
export shared_note

export marks that variable for inclusion in the environment of subsequently started commands. Inspect only that entry:

Terminal window
printenv shared_note
Captured output
ready

printenv 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?

Terminal window
bash -c 'printf "after: local=[%s] shared=[%s]\n" "$local_note" "$shared_note"'
Run it, then compare with your guess
Terminal window
bash -c 'printf "after: local=[%s] shared=[%s]\n" "$local_note" "$shared_note"'
Captured output
after: local=[] shared=[ready]

Only shared_note crosses. Export did not select local_note, and it did not retroactively update any already running child.

Export selects values for a child The parent has local_note=private and shared_note=ready. After export shared_note, a child inherits shared_note but not local_note. Parent shell local_note=private; shared_note=ready Select the environment entry export shared_note Child process shared_note=ready; no local_note
Export selects values for a child The parent has local_note=private and shared_note=ready. After export shared_note, a child inherits shared_note but not local_note. Parent shell local_note=private; shared_note=ready Select the environment entry export shared_note Child process shared_note=ready; no local_note
Simplified model. The parent keeps both values; export shared_note selects only shared_note for a subsequently started 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.

The next command string contains two commands separated by ;. The child executes them in order: assign its own value, then print it.

Terminal window
bash -c 'shared_note=child; printf "%s\n" "$shared_note"'
Captured output
child

Check the parent at your prompt:

Terminal window
echo "$shared_note"
Captured output
ready

The 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.

B05.3 used cd - and mentioned OLDPWD. Set two known locations:

Terminal window
cd first
cd ../second
Terminal window
echo "$OLDPWD"
Captured output
/home/learner/lab/b08/variables/first

OLDPWD holds the previous working directory, maintained by Bash when you change directories. cd - uses it:

Terminal window
cd -
Captured output
/home/learner/lab/b08/variables/first

The command returns to first and prints its path. Return to the lesson directory before the challenge:

Terminal window
cd ~/lab/b08/variables

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.

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
Terminal window
handoff='review complete'
export handoff
Terminal window
bash -c 'printf "[%s]\n" "$handoff"'
Captured output
[review complete]
Terminal window
bash -c 'handoff=checked; printf "%s\n" "$handoff"'
Captured output
checked
Terminal window
printf '%s\n' "$handoff"
Captured output
review complete

The child’s checked did not replace the parent’s review complete.

Remove only the practice variables from this shell:

Terminal window
unset name local_note shared_note handoff

unset removes these named variables. Do not unset HOME, PATH, or unrelated values. Then inspect and remove this lesson’s disposable directories:

Terminal window
cd ~/lab/b08
ls -AR variables
Terminal window
rm -r ~/lab/b08/variables

Rerun 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.
  • export selects variables for subsequently started processes; printenv NAME inspects a specific inherited entry.
  • A child can change its own values without rewriting the parent’s variables.
  • Bash maintains OLDPWD, which supplies the destination for cd -.

Next, PATH and command resolution follows an environment value Bash uses to locate programs.