← All lessons
Processes and scripts · Lesson 3 of 7

Variables and the path

The shell can store values in variables. Set one with name=value (no spaces around the =), and read it with a $ in front.

$ duck=mallard
$ echo "I saw a $duck"
I saw a mallard
$ echo 'I saw a $duck'
I saw a $duck

Double quotes fill in variables; single quotes don't. Wrap variables in double quotes when you use them ("$file"), so a value with spaces stays one argument. When a name runs into other text, use braces: "${duck}s" gives mallards.

Some variables are set for you and passed on to every program you start; these are environment variables, written in capitals by convention. HOME is your home folder, USER your name, and PATH is the list of folders the shell searches for programs. env lists them all. export turns your own variable into one, so programs you run can see it: export EDITOR=nano.

$ echo $PATH
/home/sam/.local/bin:/usr/local/bin:/usr/bin:/bin

When you type ls, the shell looks through each folder in PATH, in order, and runs the first ls it finds. That's why a program in your current folder needs ./ in front: the current folder isn't on the path, on purpose. To add a folder for good, put export PATH="$HOME/bin:$PATH" in ~/.bashrc, the file Bash reads each time it starts.

$( ) runs a command and drops its output in place: today=$(date +%F) stores today's date, like 2026-10-21.

Practice

Set a variable called name to dana.

More info

Quick reference

name=valueSet a variable (no spaces).
$name, "$name", ${name}Use it; keep it one argument; mark where the name ends.
export NAME=valuePass it on to programs.
HOME, USER, PATHYour folder, your name, where programs are found.
envList environment variables.
$(cmd)The command's output, in place.
~/.bashrcRun each time Bash starts.

The other Linux courses