SkillByAIOpen interactive version →

Lesson 21 / 31

The PATH Variable

Understand how PATH helps the shell find commands and add your own scripts directory to it.

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?

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

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