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.
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.
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.
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.
nope-cmd; echo "status=$?"
type -t printf lsThe 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.