Environment variables in Linux: env and export

Updated November 1, 2026

An environment variable is a name-value pair that a process passes down to its children.

Common cases

env                  # all environment variables
echo "$HOME"          # the value of one variable
export EDITOR=vim    # create and export
NAME=value command  # set a variable for one command only

Sample output

$ x=5
$ bash -c 'echo "[$x]"'
[]
$ export x
$ bash -c 'echo "[$x]"'
[5]
$ env | grep -E "^(HOME|PATH|x)="
x=5
HOME=/home/student
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

How it works: export and inheritance

A plain variable x=5 lives only in the current shell. export x marks it as an environment variable, and then every process started from that shell (its children) receives it. Inheritance goes one way, down: a child cannot change its parent's environment. That is why settings like EDITOR or PATH are exported, so the programs you launch can see them.

Common mistakes

There must be no spaces around the =. Bash reads NAME = value as a command NAME with the arguments = and value, and answers bash: NAME: command not found. A value with a space needs quotes too: GREETING=hello world assigns only hello to GREETING, then tries to run world as a command and prints bash: world: command not found.

A typo in a variable name raises no error: a name that does not exist expands to an empty string. You set PREFIX, then wrote $PREFX in a script, and echo "rm -rf $PREFX/data" prints rm -rf /data. The path now starts at the root, and that is a different command. A set -u line at the top of the script catches it: the same spot then fails with bash: PREFX: unbound variable.

env lists only the exported variables, not every variable. After y=7, the command env | grep "^y=" finds nothing and returns exit code 1, while echo $y prints 7. Every shell variable, exported or not, shows up in set. When a variable is there in echo but missing from env, the export is missing too, and the program you launch will not see it.

Used in

You need this in the lesson The shell environment. The theory is the chapter The shell environment and configuration.

See also

The PATH variable, The ~/.bashrc file.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.