which, type, command -v
What exactly runs for a command name — a file from PATH, a builtin, an alias or a function
When you type ls, the shell looks up the name in order: alias → function → builtin → executable file in the directories from $PATH. Three tools answer the question "what will run":
type— a bash builtin that knows about everything: aliases, functions, builtins, files;command -v— the same, briefly and per POSIX, works in anysh; the best choice for scripts;which— a separate program that searches only files in$PATHand knows nothing about what's defined in the shell.
Syntax
type [-afptP] name…
command -v name
command -V name
which [-a] [-s] name…
type
| Option | What it does |
|---|---|
| (no options) | human-readable: ls is /usr/bin/ls, cd is a shell builtin, ll is aliased to 'ls -l', the function's body |
-t | one word: alias, function, builtin, keyword, file; empty and code 1 if not found |
-a | all variants in order: alias, builtin and all files in PATH |
-p | the file path, only if the name would run a file (empty for a builtin) |
-P | the file path in PATH, even if there is a builtin or alias with the same name |
-f | ignore functions |
keyword means reserved words of the syntax: if, for, while, [[. "ls is hashed (/usr/bin/ls)" means the path has already been remembered (see hash below).
command
| Form | What it does |
|---|---|
command -v name | the file path; for a builtin, function or keyword — the name itself; for an alias — the line alias ll='ls -l'. Not found — empty and code 1 |
command -V name | a verbose description, like type |
command name args | run it, bypassing aliases and functions with that name |
command -p … | search the standard PATH rather than the current $PATH |
which
| Option | What it does |
|---|---|
-a | all matches in all PATH directories, not just the first |
-s | print nothing, just the exit code |
Exit code: 0 — all names found, 1 — at least one not found (the found ones are still printed).
hash — the path cache
Bash remembers where it found a command and doesn't search PATH for it next time.
| Command | What it does |
|---|---|
hash | the table: how many times each was run and from where |
hash -r | forget everything — after installing, moving or removing programs |
hash -d name | forget one name |
hash -t name | show the remembered path |
Examples
# what ls, cd and ll are in this shell
type ls cd ll
type -t cd # builtin
# all echoes: the builtin and the file
type -a echo
# check in a script that a program is installed
if ! command -v jq >/dev/null 2>&1; then
echo "install jq" >&2
exit 1
fi
# full path to a program (for sudoers, cron, systemd)
command -v systemctl
# where a command really leads through a chain of links
readlink -f "$(command -v python3)"
# which copies exist in different PATH directories — who "shadows" whom
which -a python3
# call the real ls, bypassing an alias or function named ls
command ls
\ls
# after moving a program to another directory, the shell looks for it at the old path
hash -r
Pitfalls
whichdoesn't see aliases, functions and builtins.which cdprints nothing and returns 1, andwhich echoshows/usr/bin/echo, although bash will run the builtinecho. To find out what really runs, usetype.whichbehaves differently on different systems (on Debian/Ubuntu it's a script, on other distributions a binary with different options and messages). In scripts usecommand -vinstead.- A stale cache. If a program was moved, bash keeps running it from the old path: "No such file or directory" with code 127, although
whichfinds it in the new place.hash -ror a new shell helps. - The first match in PATH wins. A file in
/usr/local/binshadows one with the same name in/usr/bin. If "the wrong version" runs —type -a name. command -vfor an alias prints the linealias ll='ls -l', not a path. If you specifically need the file —type -P name.whereisis a different tool: it looks for the binary, sources and man pages in standard directories, not in your PATH.