Skip to content

PATH and command resolution

Module
How the Shell Interprets Input
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.4 LTS (course VM), GNU Bash 5.2.21; Ubuntu 24.04.5 LTS (lab container)
On this page

You are in an empty practice directory, yet head works by name. Its program file is somewhere else. Changing directories does not explain how Bash finds it, and listing the current directory will not reveal the search.

By the end, you can read the search order, explain which command Bash selects, and recognize when a name bypasses that search.

Finish Variables and environment basics (B08.3). Use only this empty practice directory:

Terminal window
mkdir -p ~/lab/b08/path
cd ~/lab/b08/path

Keep your existing lab work. Use Reset this lesson before repeating the setup. This lesson does not change PATH or create executable files.

The PATH and command-location output below was captured on the course’s Ubuntu 24.04.4 Desktop VM in an interactive Bash. Your PATH can differ with installed software and account configuration. The separate lab capture verifies this lesson’s setup and reset, not the VM’s search path.

B04.4 introduced PATH as the list Bash searches for programs. B08.3 explained environment entries. Inspect this particular entry:

Terminal window
printenv PATH
Captured on the course VM
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

PATH is one value containing directory names separated by colons. Read it from left to right. The first entry is /usr/local/sbin, then /usr/local/bin, then /usr/sbin, then /usr/bin, and so on. A colon separates entries; it is not part of a directory name.

The captured value ends with /snap/bin:/snap/bin. Listing a directory twice is harmless because the search stops at the first match.

For an external command name without a slash, Bash looks for an executable file with that name in the PATH entries in order. It skips entries that do not supply a matching executable. A PATH entry does not promise that its directory exists or contains the command you requested.

The current working directory is not automatically searched merely because you are standing in it. It would need to be selected by PATH, or addressed through a path containing a slash. This distinction explains how a program stored elsewhere can run from the empty lesson directory.

B04.4 already taught type and command -v. Use them to check the result of the search:

Terminal window
command -v head
Captured on the course VM
/usr/bin/head
Terminal window
type head
Captured on the course VM
head is /usr/bin/head

The selected file is /usr/bin/head. The earlier PATH entries did not supply a matching executable for this lookup. Once Bash finds that match, later entries do not replace it.

PATH search for head Static simplified model of the captured PATH: search the first three entries in order, then select /usr/bin/head. Later entries are not needed for this lookup. 1: /usr/local/sbin Continue the search for head 2: /usr/local/bin Continue the search for head 3: /usr/sbin Continue the search for head 4: /usr/bin Select /usr/bin/head; stop
PATH search for head Static simplified model of the captured PATH: search the first three entries in order, then select /usr/bin/head. Later entries are not needed for this lookup. 1: /usr/local/sbin Continue the search for head 2: /usr/local/bin Continue the search for head 3: /usr/sbin Continue the search for head 4: /usr/bin Select /usr/bin/head; stop
Simplified model. For head, the captured PATH reaches /usr/bin and selects /usr/bin/head; the remaining entries are not needed.

In words: the search considers /usr/local/sbin, /usr/local/bin, and /usr/sbin before /usr/bin. The selected head is in /usr/bin, so the search stops there. This static model explains the order; it is not a recording of Bash’s internal search operations. Bash can also remember a previously found location, as the next section explains.

Distinguish all locations from the selected location

Section titled “Distinguish all locations from the selected location”

Predict first: if type -a head lists another path after /usr/bin/head, does Bash run both copies?

Terminal window
type -a head
Run it, then compare with your guess
Terminal window
type -a head
Captured on the course VM
head is /usr/bin/head
head is /bin/head

No. -a asks type to report all applicable locations, rather than only the selected answer. It does not execute head at either path.

The /bin and /usr/bin entries refer to the same directory on this VM, as B05.4 explains.

If you have already run an external command in this terminal, type can describe its path as hashed, meaning Bash remembers where it found it. B04.4 introduced this wording. It is a cached location, not another kind of program; do not assume that the search must be repeated from the beginning on every invocation.

Predict first: does the presence of a printf file on PATH mean that typing printf selects that external file?

Terminal window
type -a printf
Run it, then compare with your guess
Terminal window
type -a printf
Captured on the course VM
printf is a shell builtin
printf is /usr/bin/printf
printf is /bin/printf

No. For this name, Bash’s builtin is selected before its external alternatives. The external files still exist, and -a reports them too.

The existing alias is another part of resolution:

Terminal window
type -a ls
Captured on the course VM
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls

As in B04.4 and B08.1, Bash replaces the ls alias with ls --color=auto, then resolves the external program. An alias is not another directory entry. Shell functions are another possible category, covered beyond these examples; this lesson does not assume that every command on every account belongs only to the categories shown here.

Inspection What it establishes here
printenv PATH The exported directory search order
command -v head The selected external location
type -a head All reported executable locations for that name
type -a printf Builtin first, external alternatives afterward
type -a ls Alias first, then reported file locations

An explicit command path contains a slash. Bash uses that path instead of searching PATH for a bare command name:

Terminal window
/usr/bin/printf '%s\n' 'explicit path'
Captured on the course VM
explicit path

This selects the external printf file at the stated location, rather than Bash’s builtin. The same principle applies to a relative path containing a slash. The path identifies what to run; it does not create a missing file or grant permission to execute it.

Treating PATH as one directory. Its colons separate directory entries. Read each entry independently and preserve the order.

Assuming every listed location runs. type -a reports alternatives. A normal invocation selects one command.

Copying another account’s PATH to fix a typo. B04.4’s first response to a misspelled command remains checking the name. Replacing PATH can remove directories your account needs.

An earlier PATH entry can select a different executable with the same name. On an account you did not configure, use type to inspect the selected command before trusting a familiar name. An explicit path selects that file, but does not by itself prove that the file is trustworthy.

Goal: Identify the file selected by cat, then explain where its containing directory occurs in the captured PATH. Use both forms of inspection from B04.4.

Hint

One command gives a sentence; the other gives the selected result in a shorter form. Compare the parent directory with the colon-separated entries.

Solution
Terminal window
type cat
Captured on the course VM
cat is /usr/bin/cat
Terminal window
command -v cat
Captured on the course VM
/usr/bin/cat

/usr/bin is the fourth entry of the captured PATH. This result identifies the selected program file; it does not mean cat lives in the empty working directory.

Return to the parent and inspect only this lesson’s directory:

Terminal window
cd ~/lab/b08
ls -AR path
Terminal window
rm -r ~/lab/b08/path

Rerun the setup under Before you start. If the directory is already absent, skip removal. Close any program using this directory first.

  • PATH lists directories separated by colons, in search order.
  • A bare external name selects the first matching executable, with remembered locations possible after prior runs.
  • Aliases and builtins can determine the result before external lookup; type -a exposes alternatives.
  • A command containing a slash uses that path instead of a PATH search.

Next, Wildcards and simple expansions explains how Bash can turn one pattern into several filename arguments.