Linux Command Line · Lesson 29 of 43
Environment variables and PATH
Learn shell variables, environment variables and export, how PATH decides which program runs, and how to add your own folder to it.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will understand how settings pass from your shell to the programs it starts, why the shell finds ls but not your own script, and how to fix that. Environment variables and PATH explain many "command not found" errors and are the basis of configuration on every Linux system.
Shell variables
A variable stores a piece of text under a name. Set it with NAME=value and read it with a dollar sign. There must be no spaces around the equals sign:
greeting="Hello, Linux"
echo $greeting
echo "$greeting"
echo "${greeting}!"Hello, Linux
Hello, Linux
Hello, Linux!Quote the value if it contains spaces. The braces in ${greeting} mark where the name ends, which matters when text follows directly. By convention, variables you set yourself use lowercase names, and system-wide ones use UPPERCASE. Remove one with unset greeting.
Environment variables
A plain shell variable belongs to the current shell only. Programs you start do not see it. An environment variable is a variable that is copied into every program you start. Turn a variable into one with export:
colour=blue
bash -c 'echo "child sees: [$colour]"'
export colour
bash -c 'echo "child sees: [$colour]"'child sees: []
child sees: [blue]The first child printed nothing for the variable, and the second saw it. A child gets a copy: if the child changes the value, your shell is not affected. Export and assignment can be combined: export colour=blue. A variable for one command only goes in front of the command:
GREETING=hola bash -c 'echo "$GREETING"'
echo "after: [$GREETING]"hola
after: []Reading the environment
env and printenv list the environment, and printenv NAME prints one value. Some variables you will see on any system:
| Variable | Meaning |
|---|---|
HOME |
Your home folder |
USER |
Your user name |
SHELL |
Your login shell |
PWD |
The current folder |
LANG |
Language and character encoding |
EDITOR |
The editor programs should open |
PATH |
Where to find commands |
printenv HOME
echo "$USER lives in $HOME"/home/learner
learner lives in /home/learnerSome programs also read their configuration from variables: EDITOR=nano, proxy settings, API keys and the like. Never print secrets into logs, and never put them in command lines that others can see.
PATH: how commands are found
When you type ls, the shell must find a program with that name. It looks in the folders listed in PATH, separated by colons, from left to right, and runs the first match:
/home/asha/bin:/usr/local/bin:/usr/bin:/bintype and which show what was found. Our practice machine's list differs from yours, so we do not print it, but you can see it with echo $PATH | tr ':' '\n':
type -a ls | head -n 1ls is /usr/bin/lsIn your own interactive terminal the first line may instead say that ls is an alias (for example ls is aliased to ...), because many systems add colour that way. That is normal; the next line of type -a shows the real program.
The current folder is not in PATH, on purpose: otherwise a file named ls in some folder could replace the real command without your noticing. That is why you must write ./script.sh for a script in the current folder.
Adding your own folder to PATH
A personal bin folder is the tidy way to install your own scripts. Create it, put an executable script inside, and the shell cannot yet find it:
mkdir -p ~/bin
printf '#!/bin/bash\necho "hello from my own command"\n' > ~/bin/hi
chmod +x ~/bin/hi
hibash: hi: command not foundNow prepend the folder to PATH and try again:
mkdir -p ~/bin
printf '#!/bin/bash\necho "hello from my own command"\n' > ~/bin/hi
chmod +x ~/bin/hi
PATH="$HOME/bin:$PATH"
hi
command -v hihello from my own command
/home/learner/bin/hi"$HOME/bin:$PATH" puts the new folder first and keeps the old list. Writing PATH=$HOME/bin alone would replace the list and break most commands for that session. Putting the folder first means your command wins over a system command of the same name, and putting it last means it can never override one.
Making it permanent
A variable set in a terminal vanishes when the shell ends. To keep it, add the line to a startup file, for example ~/.bashrc:
export PATH="$HOME/bin:$PATH"
export EDITOR=nanoNew terminals read the file; for the current one, run source ~/.bashrc. The lesson on aliases and .bashrc covers these files in detail.
Variables and scripts
Inside scripts, variables work the same way, and ${name:-default} supplies a default when a variable is unset or empty:
unset city
echo "city: ${city:-unknown}"
city=Pune
echo "city: ${city:-unknown}"city: unknown
city: PuneCommon mistakes
Spaces around =. name = Asha runs a command called name.
Forgetting export. A child program does not see the variable.
Overwriting PATH. Always include the old value: PATH="new:$PATH".
Expecting changes to be permanent. They last only for the current shell unless saved in a startup file.
Exercise
Create ~/bin/today, a script that prints a line of your choice, make it executable, add ~/bin to your PATH for the session, and run it from any folder. Then set a variable, check whether a child bash -c can see it, export it, and check again. Finally, print your PATH with one folder per line.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.