Skip to content
BENIBIX Learn

Environment Variables

Level 1 · Typing commands

What an environment variable is, how to set and read one, and why apps keep passwords in them. What PATH does, and why a value can vanish.

Course contentsLesson 3 of 10

After this lesson you can

  • Set and read an environment variable
  • Explain what PATH is for
  • Keep a value after the window closes

10 mincovers 1 glossary entries

Worth reading first: Pipes and man Pages

What environment variables hold

An environment variable is a named value the shell hands to each program it starts. You set one with export, as in export NAME=Emma. You read it back with a $ sign, as in echo $NAME. Programs read these values to learn where things are and how to act. Nobody has to type them in each time.

Some are set for you when you log in. HOME holds your own folder. USER holds your name. LANG holds your language. You can list them all with the env command, and there are often a few dozen.

PATH is the one that matters most. It is a list of folders, joined by colons. When you type a command, the shell looks for a program with that name in each folder on the list, in order. If the program is not in any of them, you get command not found, even though it is on the machine.

Apps use these values for settings that change from one machine to the next. The same app can run on a laptop and on a server. It finds its data in a different place on each. Passwords and keys often live here too. That keeps them out of the code that gets shared.

Each program gets a copy of the values at the moment it starts. A change made later only reaches programs started after it. And when you close the window, the values you set by hand are gone.

To keep a value, write the export line into a file the shell reads at start-up. For bash that is a file in your home folder called .bashrc. Every new window will then have it.

A common mistake is to put a space around the equals sign. NAME = Emma does not set anything. The shell tries to run a command called NAME and fails. Write it with no spaces: NAME=Emma.

console
$ export NAME=Emma
$ echo "Hi $NAME"
Hi Emma
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
Set a value, read it back, then look at the list of folders the shell searches for commands.
Each program the shell starts gets a copy of these values. The copy is made as it starts.

Worked example

Jack installs a tool that will not run

Jack installs a tool into /home/jack/.local/bin. When he types its name, the shell says command not found.

echo $PATH shows that folder is not on the list. He adds it in .bashrc, opens a new window, and the tool runs.

The words for this lesson1

Check yourself

A program is on the machine, but the shell says command not found. What is the likely cause?

Finished this lesson?

Marking it complete also marks its 1 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.