Lesson 20 / 29

The PATH Variable

How the shell finds the program behind a command.

PATH is a search list

PATH is a colon-separated list of directories. When you type ls, the shell searches each directory in PATH, in order, for a matching executable.

Viewing and extending PATH

Add a directory of your own scripts so you can run them by name from anywhere.

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

Output:

/usr/local/bin:/usr/bin:/bin

Quick check: If two directories in PATH both have a program called `foo`, which one runs?

  • The one found in the first matching directory
  • Both run together
  • Linux picks randomly
Answer

The one found in the first matching directory — The shell searches PATH directories in order and stops at the first match.