$_ CLI In Depth
Learning progress 0%
lesson environment path dotfiles

Shell Variables vs Environment Variables

5 min read Module 9: Environment, PATH & Dotfiles Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:16

Two places a value can live

When you type a line such as greeting=hello at the prompt, bash stores that value in its own memory. The name greeting is now a shell variable. Inside bash you can read it with $greeting, use it in an if statement or a loop, and change it at any time. None of the programs that bash starts can see it, because a shell variable is not part of anything that gets passed on.

BASH3 lines
greeting=hello
echo "$greeting"
env | grep greeting

The first line of output is hello, because bash can read its own variable. The second command prints nothing. env prints the environment, which is the list of NAME=value strings that bash gives to each new program. The name greeting is not on that list, so grep finds no match, prints nothing, and exits with status 1.

The export attribute

The export command marks a variable so that bash copies it into the environment of every program it starts from then on. The value does not have to be set again. export greeting changes only the attribute. The value stays exactly where it was.

BASH2 lines
export greeting
env | grep greeting

Now the second command prints greeting=hello. The declare builtin shows the attribute directly. An unexported variable prints as declare -- greeting="hello", and an exported one has an x flag, as in declare -x greeting="hello". The printenv command is a shortcut for reading the environment alone: printenv greeting prints the value only when the variable is exported, and prints nothing otherwise.

Reading the two lists

set with no arguments prints every shell variable and every function that bash knows about. That is usually far more output than you need. When the question is what a child program will receive, use env or printenv, because they show only the exported variables. If a program behaves as though a setting is missing, the first check is printenv NAME, and the second check is set | grep NAME to see whether the variable exists in the shell at all.

Tip

A variable can exist in the shell and be missing from the environment at the same time. That combination is the most common cause of "it works at the prompt but not in my script".

Temporary values for one command

An assignment placed directly in front of a command gives that one command its own environment value. The shell variable itself does not change. Bash removes the temporary value when the command ends.

BASH2 lines
LANGUAGE_HINT=verbose env | grep LANGUAGE_HINT
echo "[${LANGUAGE_HINT:-unset}]"

The first command prints LANGUAGE_HINT=verbose, because env received the value. The second command prints [unset], because the shell never stored the value. This pattern is useful when you want to change one program's behavior without changing anything else.

Removing a value or the export mark

unset name removes the variable from the shell entirely. export -n name is different: it keeps the value in the shell but removes the export attribute, so later programs stop receiving it. This is handy when you want to test a program with and without a setting while keeping the shell variable around for later use.

Why the split exists

The split is older than Linux and is shared by every POSIX shell. A shell needs private variables for its own logic, such as a loop counter or a path it is building, and it would be wasteful and risky to pass all of them to every program. Programs need a much smaller and more stable set of values, such as where the home directory is, which editor to use, and where to find commands. The environment is that smaller set. Exporting is how you say that a particular name belongs in it.

This also explains why a variable can look correct at the prompt and still be absent for a program. The shell is not hiding anything from you. It is doing what the design asks, and the program sees only the list it was given.

A worked example

Suppose a script named build.sh uses a variable called BUILD_DIR, and you set it at the prompt before running the script. Whether the script sees the value depends on one word. Without export, the script finds the variable unset and falls back to its default. With export, the script uses the directory you chose. The script does not change. Only the attribute of the variable at the prompt changed.

When you are unsure what a process received, do not guess from the shell. Ask the program. The export -p command lists every variable the shell would pass on, in a form that can be read back by bash, and printenv with no arguments prints the same information as plain lines. Comparing either list with the variables you think you exported usually reveals the gap in a few seconds.

When a value should reach one run and nothing else, the prefix form is the better tool, because the shell is left exactly as it was. Choosing between plain assignment, export, and prefix assignment is mostly a question of how long the value should live and who should be able to see it. Plain assignment lives in the shell, export lives in every later program, and a prefix lives for one command.

Common mistakes

Three mistakes account for most of the confusion. The first is setting a value in the shell and then running a script that expects it, without exporting it first. The second is writing spaces around the equals sign. In name = value bash treats name as a command and = and value as arguments to it. The third is assuming that a name in one case is the same as the name in another case. Variable names are case sensitive, so Path and PATH are different variables, and only PATH affects command lookup.

Shell variables, exports and exec

Visualizer

A variable stays private to the shell until it is exported. A child gets a copy of the exported set when it starts, and nothing a child changes comes back.

1 / 4
Shell memory
A1 (shell variable)
B2 (shell variable)
Environment block
B2 (exported)

export B copies B into the environment block. A stays only in the shell’s own memory. The environment block is what a program receives when it starts.

A=1; export B=2; bash -c 'echo "A=[$A] B=[$B]"'
Terminal lab · 2 steps · real Linux in your browser

Shell variables and the environment

Show that a plain shell variable stays in bash, that an exported one reaches a child, and that printenv lists only exported names.

01
A plain variable does not reach a child
Create vars-demo.sh in /workspace. It sets one plain variable and one exported variable, then starts a child bash that prints both: ``bash #!/bin/bash PLAIN=one export SHARED=two bash -c 'echo "PLAIN=${PLAIN:-unset}"; echo "SHARED=${SHARED:-unset}"' ` Run bash vars-demo.sh. You should see PLAIN=unset and SHARED=two`.
Show hint for step 1
Only the exported variable is copied into the environment that the child bash receives.
02
Use printenv to test a variable
Create printenv-demo.sh in /workspace. It checks whether a plain variable is in the environment, then prints an exported one: ``bash #!/bin/bash X=local export Y=exported if printenv X >/dev/null; then echo "X is in the environment"; else echo "X is not in the environment"; fi printenv Y ` Run bash printenv-demo.sh`. The first line says X is not in the environment, and the second line is exported.
Show hint for step 2
printenv prints a variable only when it is in the environment, so it is the right test for export.
Check yourself Question 1 of 4
Concept Check Listing exported variables recall

Which command lists only the variables that a child program will receive?

Recap & Key Takeaways

A shell variable lives in bash. Only exported variables are copied into the environment that child programs receive.
export changes the attribute, not the value. declare -p shows the x flag on exported variables.
A prefix assignment such as NAME=value command gives one command a value without changing the shell.
When a program ignores a setting, check printenv first and set second.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue