Environment variables and PATH
Environment variables are named settings that every program inherits from the shell. PATH is the one that tells the shell where to look for commands.
Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?
An environment variable is a name with a text value, like HOME=/home/ana. When your shell starts a program, it passes a copy of its environment along, so programs can read settings such as your home folder, your preferred editor or an API endpoint without you typing them every time.
Looking at variables
echo "$HOME" # your home directory
echo "$USER" # your username
echo "$SHELL" # your login shell
env | sort # every exported variable
printenv PATH # one variable
Setting variables
There is a difference between a plain shell variable and an exported one. Only exported variables are passed to programs you start.
GREETING=hello # shell variable, no spaces around =
echo "$GREETING"
export API_URL=http://localhost:8000 # visible to child programs
python3 -c 'import os; print(os.environ["API_URL"])'
DEBUG=1 npm test # set only for this one command
unset GREETING # remove it
Spaces matter: NAME = value is a common mistake. The shell reads that as running a command called NAME.
What PATH does
When you type python3, the shell does not search your whole disk. It looks in each folder listed in PATH, from left to right, and runs the first match.
$ echo "$PATH"
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
$ command -v python3
/usr/bin/python3
$ type ls
ls is /bin/ls
command -v and type show which file will actually run. When two versions of a tool are installed, the one in the folder that appears earlier in PATH wins.
Adding a folder to PATH
Tools installed per user often live in ~/.local/bin or ~/bin. To add one for the current session:
export PATH="$HOME/.local/bin:$PATH"
Putting the new folder first gives it priority; putting it last ("$PATH:$HOME/.local/bin") makes it a fallback. Always keep the existing $PATH in the value, or every normal command will suddenly be “not found”.
Making it permanent
Changes made with export disappear when you close the terminal. To keep them, add the line to your shell’s startup file:
- zsh (the default on macOS):
~/.zshrc - bash on Linux:
~/.bashrc - bash login shells, including bash on macOS:
~/.bash_profile, which commonly sources~/.bashrc
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc # reload without opening a new terminal
When you see "command not found"
Messages like bash: foo: command not found or zsh: command not found: foo mean none of the PATH folders contains foo. Check, in this order:
- Is it spelled right, and is it actually installed?
- Where did the installer put it? Look for its
binfolder, for example withls ~/.local/bin. - Is that folder in
echo "$PATH"? If not, add it as shown above. - Did you open a new terminal or
sourceyour startup file after editing it? - For a script in the current folder, use
./script.sh. The current directory is deliberately not in PATH.
Keep secrets out of your history
Values typed on the command line, like export TOKEN=..., are saved in your shell history. For secrets, prefer a .env file that your tool loads and that is listed in .gitignore, or your operating system’s keychain.
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
See also
- 0049 Pipes and redirection: connecting commands A pipe sends one command's output into another command, and redirection sends output to a file. Together they let small tools build… Beginner · Explains an idea · Linux, macOS Article Basics · Text processing 15 Oct 2026
- 0050 File permissions: chmod, chown and what 755 means Every file has an owner, a group and read/write/execute permissions for three kinds of users. chmod changes the permissions and chown changes… Beginner · Explains an idea · Linux, macOS Article Basics 15 Oct 2026
- 0057 Shell cheat sheet: everyday commands A quick reference for moving around, managing files, viewing and searching text, checking processes and disk space in bash and zsh, with… Beginner · Cheat sheet · Linux, macOS Article Basics · Command-line tools 15 Oct 2026
- 0284 What is a shell? A shell is the program that reads the commands you type and tells the operating system to run them. Beginner · Explains an idea · Any system Article Basics 15 Oct 2026
- 0285 What is a terminal? A terminal is the window where you type commands; the shell inside it does the actual work. Beginner · Explains an idea · Any system Article Basics 15 Oct 2026
- 0286 What is PATH? PATH is the list of folders your shell searches to find a program when you type its name. Beginner · Explains an idea · Any system Article Basics 15 Oct 2026
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Dominik +3,281 chars