The PATH and how commands are found

Beginner · 15 min

See how the shell finds a command by walking $PATH in order, why ./script needs the dot, how the hash cache hides changes, and how shadowing works.

Before thisMoving around the filesystem

Learn Linux TV · 22 min · from 2:53

The PATH and how commands are found

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Learn Linux TVReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • $PATH is a colon-separated list of directories. For a command with no slash in it, the shell checks each directory in order and runs the first match.
  • A name containing a slash, such as ./tool or /usr/bin/ls, skips the search entirely. That is why a script in the current directory needs ./.
  • type -a and command -v show what a name resolves to. type -a lists every match in search order.
  • Bash remembers where it found a command (the hash table), so a new file earlier in PATH can be ignored until you run hash -r.
  • Putting a directory early in PATH lets its files shadow system commands. Keep . out of PATH and add your own bin directory with export PATH="$HOME/bin:$PATH".

The problem: a name is not a location

You type ls. The shell has to run a file, but you only gave it a name. Somewhere there is a file called ls, in /bin as it happens. How does the shell find it without you typing /bin/ls?

Imagine the alternative: you would write full paths for every command. The shell instead keeps a list of places to look, which you can print:

$ echo "$PATH"
/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin
$ echo "$PATH" | tr ':' '\n' | nl
     1	/opt/homebrew/bin
     2	/usr/bin
     3	/bin
     4	/usr/sbin
     5	/sbin

PATH is a plain string. Directories are separated by colons. When you give a bare name, the shell goes down the list, top to bottom, asking “is there an executable file with this name here?” The first hit runs, and later directories aren’t consulted. It’s a phone book searched in order, stopping at the first match.

The rule has a catch. If the word contains a slash, the shell treats it as a path and does no search at all. So ./hi and ../bin/hi run exactly the file named. A bare hi triggers the search. To see this I made a script and tried it three ways:

$ printf '#!/bin/sh\necho "hello from my own tool"\n' > bin/hi; chmod +x bin/hi
$ hi
bash: hi: command not found
$ cd work; ../bin/hi
hello from my own tool

The file exists and is executable, but bin isn’t in PATH, so a bare hi is not found. This is also why a script in your current directory needs ./: the current directory is not on the list. Prepend your directory to the list and the bare name works:

$ export PATH="$PWD/bin:$PATH"
$ hi
hello from my own tool
$ type hi
hi is hashed (/SCR/lab5/bin/hi)

export makes the variable visible to programs you start, a later lesson explains why. Changing PATH this way lasts only for this shell session. Making it permanent means a startup file, which is the next lesson.

Seeing what a name resolves to

type -a NAME lists every match, in the order the shell would pick them. command -v NAME prints the single winner and works in scripts. which is a program that mostly does the same, but it doesn’t know about builtins or aliases, so prefer type:

$ command -v ls hi cd
/bin/ls
/SCR/lab5/bin/hi
cd

For cd, a builtin, command -v prints just the name since there is no file. Builtins, aliases, keywords and functions are all checked before PATH, as the previous help lesson showed.

Shadowing, and the hash table that hides it

Order matters when two directories hold the same name. I put a fake ls in the early bin directory:

$ printf '#!/bin/sh\necho "not the real ls"\n' > bin/ls; chmod +x bin/ls
$ type -a ls
ls is /SCR/lab5/bin/ls
ls is /bin/ls
$ ls
bin
work
$ hash -r; ls
not the real ls

Read that carefully. type -a correctly said the fake would win. But plain ls still ran the real one, because Bash had already looked up ls earlier in the session and remembered the answer in a table. hash shows it:

$ hash
hits	command
   2	/bin/ls

After hash -r empties the table, the next lookup walked PATH again and found the fake. If you ever install or move a program and the shell insists on the old one, run hash -r. I tested this by deleting the fake, running hash -r, and seeing the real ls return. It isn’t a macOS quirk; the hash table is a Bash feature, and it exists to save a search on every command.

Don’t put . in PATH

Putting the current directory on PATH makes mine work without ./. I tried it (PATH=".:$PATH" mine printed local), and without the dot the same bare mine failed with status 127. It is convenient and unsafe: cd into a directory someone else prepared and your next ls might run their file. The usual rule is to add a specific personal directory, such as $HOME/bin, rather than ..

About the video

Learn Linux TV’s “What is $PATH on a Linux Shell?” was published 3 November 2022, 21 minutes 52 seconds. I clipped it from 2:53 to 17:48. The description’s time codes put the “alternate universe where PATH was never invented” at 2:53 and “how to make changes to PATH survive closing your shell session” at 17:48, so the clip runs from the motivation through adding a directory, and stops before the persistence part, which belongs to the next lesson. The first 2:53 contains a sponsor segment, which I cut. I couldn’t read a transcript, so the time codes and title are all I can vouch for. It is made for Linux; the examples on a Mac are the same except for the paths. Jonathan Soma’s video on macOS PATH handling is the runner-up.

What I did not run

PATH handling in zsh was not tested. I also did not run which with its Linux flags.

Next

Setting PATH for one session is fine, but you will want it to stick. That means knowing which file Bash reads on startup: startup files and login vs interactive shells.

Check yourself

  1. 01You type deploy and get 'command not found', but ./deploy works. What is the cause and the fix?

    The directory holding deploy is not in $PATH, and the shell does not search the current directory. Run it as ./deploy, or add its directory to PATH.

  2. 02Two directories on your PATH both contain a program called python. Which one runs?

    The one in the directory that appears first in $PATH. type -a python lists both in that order.

  3. 03You created a new ls in an early PATH directory, but the old one still runs. Why?

    Bash cached the old location in its hash table. Run hash -r, or open a new shell, and the lookup happens again.

  4. 04Why is putting . in PATH considered dangerous?

    Any directory you cd into could then supply a file named like a common command, such as ls, and run it. The shell would execute whatever the directory's owner left there.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark