Frequently Asked Question

What is $PATH and how does the shell find a command?

PATH is an environment variable holding a colon-separated list of directories. When you type a bare command like ls, the shell walks that list left to right and runs the first executable file it finds with that name. A typical PATH looks like /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/chris/bin, so /usr/local/bin wins over /usr/bin if both contain a program of the same name.

Order matters. Putting ~/bin at the front of PATH lets you shadow system programs with your own versions; putting it at the back keeps it as a fallback. Three builtins help you reason about lookup: which ls prints the path of the first match, type ls reports whether ls is an alias, function, builtin, or external file, and command -v ls is the POSIX equivalent. Bash also caches recent lookups hash -r clears the cache if a binary moves and the shell keeps finding the old one.

Further reading and video