Terminal · foundation
PATH and command lookup
PATH is an ordered list of directories a shell searches when a command name contains no slash, subject to shell built-ins, functions, aliases, and cached resolutions.
Why it matters
Understanding lookup order explains command-not-found errors, unexpected versions, and security problems caused by executing a different program than intended.
Mental model
How to reason about path and command lookup
Classify the command through the shell first; if external lookup is needed, test each PATH directory from left to right and use the first matching executable.
Analogy
PATH is an ordered contact list: the shell calls the first matching name it can use, so an earlier entry can shadow a later one.
Examples
See the boundary, not just the happy path
Worked example · Inspect every resolution
type -a python3In shells such as Bash, type can reveal aliases, functions, built-ins, and all PATH matches instead of reporting only one external location.
Worked example · Add a user binary directory
PATH="$HOME/.local/bin:$PATH"Prepending makes commands in the user directory win over same-named later entries; export is needed if child processes must inherit the modified PATH.
Avoid · Current directory first
PATH=.:$PATHSearching the current directory first can execute an untrusted same-named program from whichever directory the shell happens to be in.
Common mistakes
Misconceptions to remove early
Using which as a complete explanation
which is often an external program and may not model aliases, functions, or shell built-ins; shell-aware tools such as type or command -V are more informative.
Expecting PATH to search subdirectories
Each entry names one directory to search; adding a parent directory does not recursively expose executables below it.
Quick check
Can you predict the result?
1. When two PATH directories contain executable tools named deploy, which one is normally selected?
- • The match in the earliest PATH directory
- • The newest file
- • Both run in sequence
2. Why does ./deploy not use PATH lookup?
Keep building