$_ CLI In Depth
Learning progress 0%
lesson environment path dotfiles

How PATH Lookup Works

5 min read Module 9: Environment, PATH & Dotfiles Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:12

What PATH holds

The PATH variable is a single string of directory names separated by colons. Bash reads it each time it needs to find a command that is not a builtin or a function. You can see the entries one per line with a small pipeline.

BASH1 lines
echo "$PATH" | tr ':' '\n'

A typical list starts with directories such as /usr/local/bin, followed by /usr/bin and /bin. The order matters. The directories are searched from left to right, and bash stops at the first directory that contains an executable file with the name you typed.

The order bash uses

For a bare word, bash does not go straight to PATH. The full sequence is shown below. In an interactive shell, aliases are expanded first. Functions come next, then builtins such as cd and printf, then reserved words such as if and time. Only after these checks does bash search PATH for an external program.

flowchart TB word["Bare command word"] --> al["Alias, interactive shells only"] al --> fn["Shell function"] fn --> bi["Builtin"] bi --> kw["Reserved word"] kw --> pa["Search PATH left to right"] pa --> ok["First executable match runs"]

The type builtin tells you which candidate bash would use. Running type -a echo shows both answers in order: the builtin first, then the file in a directory on PATH. The builtin wins, which is why echo in a script usually does not run the external file.

When the path is not searched

If the name you type contains a slash, bash does not search PATH at all. A command such as ./tool or /workspace/bin/tool names one file directly. This is how you run a program that is not in any PATH directory, and it is also how you bypass an entry that shadows the one you want.

Empty entries and the current directory

An empty entry in PATH means the current directory. A leading colon, a trailing colon, or a double colon in the middle all add the current directory to the search. Many security problems start from this. If you run ls in a directory that someone else controls, and that directory is on PATH, a file named ls there can run in place of the real command. Keep the current directory out of PATH and call local programs with ./ instead.

Warning

Never add . or an empty entry to PATH in a shared or unfamiliar directory. Put any directory you control at the end of PATH, and only after you have checked what is in it.

The lookup cache

After bash finds a program, it remembers the full path in a hash table, so the next run does not search PATH again. The hash builtin lists the remembered entries, and hash -r clears them. Assigning a new value to PATH also clears the table. The cache matters when you install a new program into a directory that is earlier on PATH and bash keeps running the old one. In that case, run hash -r and try again.

The cache also helps scripts stay honest. A script that depends on a tool can check for it with command -v jq >/dev/null || exit 1, which uses the same search that the shell uses and fails early with a clear status. Doing the check at the top of the script is much better than finding out halfway through that a required program was never on PATH.

Adding a directory safely

To add a directory, put it at the end of PATH unless you have a reason to put it first. A directory at the end is used only when no earlier directory has a program with the same name, so system programs keep their priority. A directory at the front overrides them silently, which causes many odd bugs. The usual form is PATH="$PATH:$HOME/bin". Quoting the whole value keeps any space in a path safe, and the check with type -a afterward confirms the result.

Putting a directory in front is still useful for a tool you want to replace on purpose, such as a newer compiler installed under your home directory. In that case, run type -a after the change, so you can see that the new location wins and that nothing else in front of it was added by accident.

Exit statuses for lookup failures

Two statuses tell you what happened when a command could not run. Status 127 means bash could not find the command anywhere it looked. Status 126 means bash found a file but could not execute it, usually because the execute bit is missing or the file is not a program. A script that prints "command not found" for a file that is clearly there is often hitting status 126 because of permissions, not a PATH problem.

BASH2 lines
nope-cmd; echo "status=$?"
type -t printf ls

The first line reports status 127. The second line prints builtin for printf and file for ls, because ls is found through PATH. Learning to read these two statuses saves a lot of time when a script behaves differently on two machines.

Putting it together

When a command runs the wrong program, ask three questions in order. Is the name an alias or a function in this shell? Is it a builtin? Which directory on PATH has it first? The type -a command answers all three at once, and command -v prints only the winning name or path, which is convenient in scripts. Changing PATH should be a deliberate act with a clear order, not a side effect of adding one more directory at the front.

Terminal lab · 3 steps · real Linux in your browser

How PATH lookup works

Show that the first PATH directory wins, classify names with type -t, and read the exit statuses for not found and not executable.

01
The first PATH directory wins
Create path-order.sh in /workspace. It makes two directories, each with a greet program, then puts second first in PATH and runs greet, saving the output: ``bash #!/bin/bash mkdir -p /workspace/tools/first /workspace/tools/second printf '#!/bin/bash\necho first\n' > /workspace/tools/first/greet printf '#!/bin/bash\necho second\n' > /workspace/tools/second/greet chmod +x /workspace/tools/first/greet /workspace/tools/second/greet PATH=/workspace/tools/second:/workspace/tools/first:$PATH greet > /workspace/which-greet.txt ` Run bash path-order.sh, then read which-greet.txt. It should say second`.
Show hint for step 1
The search goes left to right and stops at the first executable match.
02
Classify names with type -t
Create kinds.sh in /workspace. It writes the kind of three names to kinds.txt, one per line, in this order: printf, time, and ls: ``bash #!/bin/bash { type -t printf; type -t time; type -t ls; } > /workspace/kinds.txt ` Run bash kinds.sh. The file should contain builtin, keyword, and file`, one per line.
Show hint for step 2
type -t prints one word: builtin, keyword, file, alias, or function.
03
Read the not-found and not-executable statuses
Create status.sh in /workspace. It runs a name that does not exist, then runs a text file that has no execute bit. Each status goes into status.txt: ``bash #!/bin/bash cd /workspace nosuch-tool-xyz 2>/dev/null echo "missing=$?" > /workspace/status.txt printf 'hello\n' > /workspace/notexec ./notexec 2>/dev/null echo "noexec=$?" >> /workspace/status.txt ` Run bash status.sh and read status.txt. You should see missing=127 and noexec=126`.
Show hint for step 3
127 means bash could not find the name. 126 means bash found the file but could not run it.
Check yourself Question 1 of 4
Concept Check Lookup order recall

In an interactive shell, in what order does bash check a bare word such as ls?

Recap & Key Takeaways

Bash checks aliases, functions, builtins, and reserved words before it searches PATH.
PATH is searched left to right, and the first executable match wins.
An empty entry in PATH means the current directory and can run the wrong program.
Status 127 means not found, and status 126 means found but not executable.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue