Intermediate · 7 min · practise every command in the built-in terminal

The PATH Variable

PATH tells the shell where to look for commands.

View PATH

echo $PATH

Output: /usr/local/bin:/usr/bin:/bin:/home/student/.local/bin

Colons separate directories. The shell searches left to right.

Find where a command lives

which ls
which python3
which bash
whereis ls          # Also shows man pages and source
type ls             # Shows if it's a builtin, alias, or file

Add to PATH

export PATH="$PATH:/new/directory"             # Temporary (this session)
export PATH="$HOME/.local/bin:$PATH"          # Add to front (higher priority)

Make permanent

Add to ~/.bashrc:

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

Then reload:

source ~/.bashrc

Why does ./script.sh need the ./ ?

The current directory . is NOT in PATH by default for security. So to run script.sh in the current directory, you need:

./script.sh

Practice tasks

  1. Print your current PATH
  2. Find where the 'ls' command is located
  3. Temporarily add /tmp to your PATH
  4. Check what type of command 'cd' is

Commands in this lesson

Shell & Environment