Environment Variables, PATH, and Shell Configuration

Understand environment variables, how PATH lets you run commands from anywhere, and how to customise your shell with .bashrc and aliases.

~7 min Beginner

What is a variable?

A variable is a name that stores a value. You create one with name=value, and you read it by putting a $ in front of the name.

name="Alex"
echo "Hello, $name"

Two rules matter:

  • There must be no spaces around =. name = "Alex" is an error.
  • Use $ when you read a variable, but not when you set it.

Environment variables

The shell and your system already define many variables. Programs can read them to learn about their surroundings. These are called environment variables.

echo $HOME
echo $USER
echo $SHELL
echo $PWD
printenv
  • HOME is your home folder, for example /home/alex.
  • USER is your username.
  • SHELL is the path of your shell.
  • PWD is your current directory.
  • printenv lists every environment variable.

export: share a variable with programs

editor=nano
export editor
export EDITOR=nano
  • A plain variable like editor=nano exists only inside your current shell.
  • export makes a variable part of the environment, so programs and scripts you start from this shell can read it too.

By convention, exported variables are written in capital letters, such as EDITOR.

PATH: how the shell finds commands

When you type ls, the shell has to find a program called ls somewhere on disk. It searches the folders listed in the PATH variable.

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

Folders are separated by colons. The shell checks them in order, from left to right, and runs the first match it finds. If no folder has the program, you get command not found.

Two commands help you check:

which ls
type ls

which ls prints the full path that will be used, such as /usr/bin/ls. type ls also tells you whether the name is a program, a shell built-in or an alias.

Adding a folder to PATH

Suppose you keep personal scripts in ~/bin. To let the shell find them from anywhere:

export PATH="$HOME/bin:$PATH"

This builds a new list: your folder first, then the old value of PATH. Any script inside ~/bin can now be run by name.

Always keep :$PATH at the end. Writing export PATH="$HOME/bin" replaces the whole list, and commands like ls stop working until you open a new terminal.

Making changes permanent

Variables you set in a terminal disappear when you close it. To keep them, put the line in a startup file that the shell reads when it begins.

  • ~/.bashrc: read every time you open a Bash terminal.
  • ~/.zshrc: the same for zsh.
  • ~/.profile or ~/.bash_profile: read when you log in.
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

The first line appends the setting to the file. source ~/.bashrc reads the file again in the current shell, so the change applies at once. You can also write . ~/.bashrc.

Aliases: shortcuts

An alias is a short name for a longer command.

alias ll='ls -lah'
alias gs='git status'
alias update='sudo apt update && sudo apt upgrade'

Type alias on its own to list your aliases and unalias ll to remove one. Aliases made in a terminal also disappear when it closes, so add them to ~/.bashrc to keep them.

A few small ones many people like:

alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
Sign up to track your progress Mark lessons as read and pick up where you left off Sign up free to unlock this practice Your first practice lesson is free to try. Create an account to unlock the rest and save your progress