PATH is the environment variable the shell consults to find executables. When you type ls, the shell searches each directory listed in PATH (separated by colons) from left to right and runs the first match it finds. A typical value is /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.
echo $PATH
which ls # show the path of the 'ls' found
type -a ls # show all matches, including aliases
PATH="$HOME/bin:$PATH" # prepend a directory
Only directories listed in PATH are searched; running ./myscript or giving a full path is required otherwise. For security, the current directory (.) is almost never in PATH, to prevent an attacker from leaving a malicious ls in /tmp and tricking root into running it.
Common traps include: editing PATH in ~/.bashrc but not ~/.bash_profile, so it is not set in login shells; prepending too aggressively and shadowing system binaries; and forgetting that sudo resets the environment (use sudo -E or sudo env "PATH=$PATH" … to preserve it). Understanding how PATH is composed is essential for diagnosing "command not found" errors on unfamiliar systems.
Related terms: Environment Variable, Shell
Discussed in:
- Chapter 5: The Shell — Environment Variables
Also defined in: Textbook of Linux