which
Overview
which locates executables by searching the PATH environment variable and printing the path of the first match. It is a traditional convenience for interactive use. For scripts, prefer command -v (POSIX) or type -P (bash) — they integrate better with shell functions, aliases, and builtins.
Syntax
which [options] command...Implementations differ (GNU which, Debian shell script which, BusyBox). Options are not fully portable.
Common Options (GNU / common)
| Option | Description |
|---|---|
-a |
Show all matches on PATH, not just the first |
-s |
Silent; exit status only (some versions) |
--version / --help |
Version / help (GNU) |
Key Use Cases
- Interactive “where is this binary?”
- See which of several installs comes first (
-a) - Quick sanity check after modifying
PATH - Compare with shell resolution (
type)
Examples with Explanations
Basic
which python3
which ls
which docker kubectlAll matches
which -a python3
which -a javaShows shadowing (e.g. /usr/local/bin before /usr/bin).
Prefer command -v in scripts
# good for scripts
if command -v jq >/dev/null 2>&1; then
jq --version
fi
# bash: PATH only, ignore functions/aliases
if type -P jq >/dev/null; then
echo "jq binary found"
fiCompare resolution tools
type python3
type -a python3
command -v python3
which python3
which -a python3| Tool | Sees aliases | Sees functions | Sees builtins | PATH binaries |
|---|---|---|---|---|
type |
yes | yes | yes | yes |
command -v |
yes* | yes* | yes | yes |
which |
often no | no | no | yes |
*shell-dependent; still better than which for “what will run”.
Debug PATH order
echo "$PATH" | tr ':' '\n' | nl
which -a nodeSilent existence (when -s exists)
which -s docker && echo present
# portable:
command -v docker >/dev/null && echo presentNotes / Pitfalls
- Debian/Ubuntu historically shipped
whichas a shell script with different flags; don’t rely on GNU-only options in portable scripts. whichmay not know about shell aliases/functions you use interactively — misleading for “what runs when I type this”.- Hashed commands in bash (
hash -rto clear) affect speed of lookup, not usuallywhich’s external search. - Relative PATH entries (
.or empty) are a security footgun —whichwill show what they resolve to. - In login vs non-login shells,
PATHdiffers; results differ.
2026-relevant notes
- Version managers (asdf, nvm, pyenv, mise) put shims early on
PATH—which -ais useful to see real binaries behind shims. - Containers often have a minimal PATH;
command -vin entrypoints is the standard check. - Prefer documenting
command -vin runbooks and automation.
Additional Resources
man which(if present),help type,help command