PATH and command resolution
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.
Before you start
Section titled “Before you start”Finish Variables and environment basics (B08.3). Use only this empty practice directory:
mkdir -p ~/lab/b08/pathcd ~/lab/b08/pathKeep 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.
Read the search list
Section titled “Read the search list”B04.4 introduced PATH as the list Bash searches for programs. B08.3 explained environment entries. Inspect this particular entry:
printenv PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/binPATH 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.
Inspect the selected file
Section titled “Inspect the selected file”B04.4 already taught type and command -v. Use them to check the result of the search:
command -v head/usr/bin/headtype headhead is /usr/bin/headThe 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.
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?
type -a headRun it, then compare with your guess
type -a headhead is /usr/bin/headhead is /bin/headNo. -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.
Account for builtins and aliases
Section titled “Account for builtins and aliases”Predict first: does the presence of a printf file on PATH mean that typing printf selects that external file?
type -a printfRun it, then compare with your guess
type -a printfprintf is a shell builtinprintf is /usr/bin/printfprintf is /bin/printfNo. 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:
type -a lsls is aliased to `ls --color=auto'ls is /usr/bin/lsls is /bin/lsAs 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 |
Give an explicit path
Section titled “Give an explicit path”An explicit command path contains a slash. Bash uses that path instead of searching PATH for a bare command name:
/usr/bin/printf '%s\n' 'explicit path'explicit pathThis 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.
Mistakes to recognize
Section titled “Mistakes to recognize”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.
Security Angle
Section titled “Security Angle”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.
Try it yourself
Section titled “Try it yourself”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
type catcat is /usr/bin/catcommand -v cat/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.
Reset this lesson
Section titled “Reset this lesson”Return to the parent and inspect only this lesson’s directory:
cd ~/lab/b08ls -AR pathrm -r ~/lab/b08/pathRerun 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 -aexposes 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.
Further reading
Section titled “Further reading”- On your system:
help type,help command, andman bash, section COMMAND EXECUTION. - GNU Bash: Command Search and Execution, Bash Variables (
PATH), and Aliases.