PATH in Linux: where commands are found

Updated November 1, 2026

PATH is a colon-separated list of directories where the shell looks for a command by its name.

Common cases

echo "$PATH"                       # the current list of directories
which ls                           # which directory the command came from
export PATH="$HOME/bin:$PATH"      # add your own directory to the front
hash -r                            # clear the cached command paths

Sample output

$ cat ~/bin/ls
#!/bin/bash
echo my own ls
$ echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ which ls
/usr/bin/ls
$ export PATH="$HOME/bin:$PATH"
$ which ls
/home/student/bin/ls
$ ls /etc
my own ls

How it works: search order is everything

When a command is not a builtin or alias, the shell walks the PATH directories left to right and runs the first matching file it finds. So order matters: a directory near the front shadows same-named programs further along. You add your own directory like this: PATH="$HOME/bin:$PATH", keeping the old $PATH at the end, or the system loses ls, cat and everything else. Never put . at the front of PATH: it lets a stray file run in place of the real command.

Common mistakes

A script in the current directory will not run by name. check.sh answers bash: check.sh: command not found even though ls shows it: the current directory is not in PATH, and the shell looks nowhere else. Write ./check.sh - a dot and a slash turn the name into a path, and the PATH search never starts.

The shell remembers where it found a command and later misses. You ran ~/bin/tool, so that path landed in a hash table; then you moved the file and put a new version in /usr/local/bin, and tool answers bash: /home/student/bin/tool: No such file or directory. Look at the error: it names a full path you never typed, and that is the trace of the cache. hash -r clears it, and hash -t tool shows the stored entry.

which knows only about files from PATH and stays quiet about everything else. which cd prints nothing and returns exit code 1, because cd is a bash builtin and has no file. type cd answers honestly: cd is a shell builtin. which is blind to aliases and functions as well, so check with type and keep which for the question 'which directory holds this binary'.

Used in

You need this in the lesson The shell environment. The theory is the chapter The shell environment and configuration.

See also

Environment variables, which, type, whereis, The ~/.bashrc file.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.