# Environment Variables

> 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:** [Linux Command Line](https://learn.benibix.com/learn/infrastructure-linux)  
**Time:** 10 min

**After this lesson you can:**

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

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

![A box marked the shell holds four named values: HOME=/home/emma, PATH=/usr/local/bin:/usr/bin, LANG=en_GB.UTF-8 and DB_PASSWORD=s3cret, set with export NAME=value. Arrows marked a copy, at start lead to two boxes: your app, which reads DB_PASSWORD instead of a file, and any other command, which finds programs through PATH. A note says the values are gone when the window closes, unless they are saved in a file such as ~/.bashrc.](https://learn.benibix.com/diagrams/infc-env-vars.svg)

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

**Words in this lesson:** [Environment Variable](https://learn.benibix.com/term/infrastructure/environment-variable)

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

**Answer:** Its folder is not in PATH. The shell only looks for commands in the folders listed in PATH. A program anywhere else is not found unless you type its full location.

---

Source: https://learn.benibix.com/learn/infrastructure-linux/environment-variables
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
