Skip to lesson

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.

Beginner to intermediate·12 min·Processes and the system·Updated 2026-11-24

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:

bash
greeting="Hello, Linux"
echo $greeting
echo "$greeting"
echo "${greeting}!"
result
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:

bash
colour=blue
bash -c 'echo "child sees: [$colour]"'
export colour
bash -c 'echo "child sees: [$colour]"'
result
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:

bash
GREETING=hola bash -c 'echo "$GREETING"'
echo "after: [$GREETING]"
result
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
bash
printenv HOME
echo "$USER lives in $HOME"
result
/home/learner
learner lives in /home/learner

Some 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:

text
/home/asha/bin:/usr/local/bin:/usr/bin:/bin

type 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':

bash
type -a ls | head -n 1
result
ls is /usr/bin/ls

In 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:

bash
mkdir -p ~/bin
printf '#!/bin/bash\necho "hello from my own command"\n' > ~/bin/hi
chmod +x ~/bin/hi
hi
result
bash: hi: command not found

Now prepend the folder to PATH and try again:

bash
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 hi
result
hello 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:

text
export PATH="$HOME/bin:$PATH"
export EDITOR=nano

New 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:

bash
unset city
echo "city: ${city:-unknown}"
city=Pune
echo "city: ${city:-unknown}"
result
city: unknown
city: Pune

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

0 / 4 understood
01 Which line sets a variable correctly in Bash?
02 What does export do?
03 What is PATH?
04 Why does prepending a folder to PATH with PATH="$HOME/bin:$PATH" work?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses