← Full reference

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 any sh; the best choice for scripts;
  • which — a separate program that searches only files in $PATH and knows nothing about what's defined in the shell.

Syntax

type [-afptP] name…
command -v name
command -V name
which [-a] [-s] name…

type

OptionWhat 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
-tone word: alias, function, builtin, keyword, file; empty and code 1 if not found
-aall variants in order: alias, builtin and all files in PATH
-pthe file path, only if the name would run a file (empty for a builtin)
-Pthe file path in PATH, even if there is a builtin or alias with the same name
-fignore 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

FormWhat it does
command -v namethe 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 namea verbose description, like type
command name argsrun it, bypassing aliases and functions with that name
command -p …search the standard PATH rather than the current $PATH

which

OptionWhat it does
-aall matches in all PATH directories, not just the first
-sprint 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.

CommandWhat it does
hashthe table: how many times each was run and from where
hash -rforget everything — after installing, moving or removing programs
hash -d nameforget one name
hash -t nameshow 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

  • which doesn't see aliases, functions and builtins. which cd prints nothing and returns 1, and which echo shows /usr/bin/echo, although bash will run the builtin echo. To find out what really runs, use type.
  • which behaves differently on different systems (on Debian/Ubuntu it's a script, on other distributions a binary with different options and messages). In scripts use command -v instead.
  • 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 which finds it in the new place. hash -r or a new shell helps.
  • The first match in PATH wins. A file in /usr/local/bin shadows one with the same name in /usr/bin. If "the wrong version" runs — type -a name.
  • command -v for an alias prints the line alias ll='ls -l', not a path. If you specifically need the file — type -P name.
  • whereis is a different tool: it looks for the binary, sources and man pages in standard directories, not in your PATH.

See also

variables · env · functions · ln · sudo · exit-codes