shellstreak

Environment variables and PATH in bash

Updated · Every command on this page ran in our practice shell

Short answer

Set a variable with NAME=value and read it with "$NAME". export NAME=value passes it on to scripts and programs you start. PATH is the list of folders the shell searches for commands; add one with export PATH="$PATH:$HOME/bin", and put that line in ~/.bashrc to keep it.

Variables hold values your commands can reuse, and the environment is the set of variables every program you start inherits. The most important one is PATH, the list of folders the shell searches for commands. The examples below ran in a practice shell; try them in the mini terminal.

Set and read a variable

Write NAME=value with no spaces around =, and read it with $NAME. Quote values that contain spaces, and quote the variable when you use it, "$NAME", so the value stays one word. Single quotes print $NAME literally.

example
learner@shellstreak:~/bakery$ SHOP="Pebble Bakery"; echo "Welcome to $SHOP"
Welcome to Pebble Bakery

Export a variable to scripts and programs

A plain variable exists only in the current shell. export NAME=value adds it to the environment, so every script and program you start from this shell receives a copy. A child can't change the parent's copy.

example
learner@shellstreak:~/bakery$ export BAKER=Sam && ./scripts/greet.sh
Hello, Sam! The ovens are on.

List the environment with env

env (or printenv) prints every environment variable. Filter it with grep to check one. unset NAME removes a variable completely.

example
learner@shellstreak:~/bakery$ env | grep -E '^(HOME|USER|PATH)='
HOME=/home/learner
PATH=/usr/local/bin:/usr/bin:/bin
USER=learner

What PATH does

When you type a command name, the shell looks in each folder in PATH, left to right, separated by :, and runs the first match. The current folder isn't in PATH, which is why you run your own scripts as ./script.sh.

example
learner@shellstreak:~/bakery$ echo $PATH
/usr/local/bin:/usr/bin:/bin

Find out what a command is: which and type

which grep prints the file PATH finds. type is more complete: it also knows about builtins (like cd), aliases and functions.

example
learner@shellstreak:~/bakery$ type cd ls
cd is a shell builtin
ls is /usr/bin/ls

Add a folder to PATH

Append your folder to the existing value, keeping what's there: export PATH="$PATH:$HOME/bin". Setting PATH=$HOME/bin on its own would hide every normal command. To keep the change in new terminals, add the same line to ~/.bashrc (or ~/.zshrc for zsh) and open a new terminal or run source ~/.bashrc.

example
learner@shellstreak:~/bakery$ export PATH="$PATH:$HOME/bakery/bin" && opening-hours
Mon-Fri 07:00-18:00
Sat 08:00-14:00
Sun closed

Short names with alias

alias ll='ls -l' makes ll run ls -l. Aliases also last only for the current shell unless you put them in ~/.bashrc. type ll shows what an alias stands for.

example
learner@shellstreak:~/bakery$ alias menu='cat menu.txt' && type menu
menu is aliased to `cat menu.txt'

Exit codes, &&, || and ;

Every command ends with an exit code: 0 for success, anything else for failure, and $? holds the last one. a && b runs b only if a succeeded, a || b only if it failed, and a ; b runs both regardless. true and false are commands that just succeed or fail, so false || echo failed prints failed. a && b || c looks like if/else but isn't: c also runs when b fails (shellcheck calls this SC2015). Use if when that matters.

example
learner@shellstreak:~/bakery$ ls menu.txt && echo found || echo missing
menu.txt
found

Processes: ps and kill

Every program you start is a process with an ID (PID), and it inherits a copy of your environment. ps aux lists all processes; pgrep name prints matching PIDs. kill PID asks a process to stop (SIGTERM) so it can clean up; kill -9 PID forces it (SIGKILL) and is the last resort. In the terminal, Ctrl-C interrupts the foreground program, Ctrl-Z pauses it, and jobs, fg and bg manage paused ones.

example
learner@shellstreak:~$ grep nginx ps.txt | awk '{print $2}'
612
613
614

SSH keys and permissions

ssh deploy@server logs you in to another machine's terminal as the user deploy. ssh-keygen -t ed25519 creates ~/.ssh/id_ed25519 (private: never share or paste it anywhere) and ~/.ssh/id_ed25519.pub (public: give it to servers and GitHub). SSH refuses a private key others can read, so use chmod 700 ~/.ssh and chmod 600 ~/.ssh/id_ed25519. If ssh warns REMOTE HOST IDENTIFICATION HAS CHANGED, stop and verify the server's new key with whoever runs it before you connect.

example
learner@shellstreak:~$ chmod 700 .ssh && chmod 600 .ssh/id_ed25519 && ls -la .ssh
total 20
drwx------ 2 learner learner 4096 Feb 27 07:30 .
drwxr-xr-x 3 learner learner 4096 Feb 27 07:30 ..
-rw------- 1 learner learner   99 Feb 27 07:30 id_ed25519
-rw-r--r-- 1 learner learner   81 Feb 27 07:30 id_ed25519.pub
-rw-r--r-- 1 learner learner   77 Feb 27 07:30 known_hosts

Check yourself

One real exercise from the “Environment & control” unit. No account needed.

bakery/bin holds the shop's own commands, like opening-hours. Add the folder $HOME/bakery/bin to the end of your PATH, then prove it worked with which opening-hours.

What's checked: the output of your last command.

Common mistakes

  • Putting spaces around =. NAME = value runs a command called NAME.

  • Overwriting PATH instead of extending it. Always include $PATH in the new value.

  • Putting a downloads or temp folder first in PATH. A file there called ls or git would run instead of the real one.

  • Setting a variable without export and expecting a script to see it.

  • Editing ~/.bashrc and expecting the terminal you're in to change. Open a new one, or run source ~/.bashrc.

Questions

What's the difference between a shell variable and an environment variable?+

A shell variable exists only in the shell where you set it. An environment variable has been exported, so programs and scripts started from that shell get a copy.

How do I add a folder to PATH permanently?+

Add `export PATH="$PATH:/path/to/folder"` to `~/.bashrc` (bash) or `~/.zshrc` (zsh), then open a new terminal.

Why do I need ./ to run a script in the current folder?+

The shell only searches PATH for bare command names, and the current folder isn't in PATH for safety. `./script.sh` is a path, so it runs that exact file.

Do exported variables reach programs I start, like a coding agent?+

Yes: every exported variable is copied into every program you launch. Don't start tools from a shell that holds production keys; `unset` them first or use a clean terminal.

Reading is step one. Now make it stick.

Practise this in a five-minute lesson with instant feedback. No account needed.

Practise it now