Examples
A reliable "does this command exist" check in a script:
Find out which binary will run, whether it's a builtin or an alias, where it lives, and its man page.
type -a cmd
alias/builtin/function/file — every match
command -v cmd
a path or nothing (for scripts)
which cmd
just the path to the file
whereis cmd
binary + man page + sources
When you type a command, the shell searches for it in the directories in
$PATH (see env-vars). These commands answer the question "what
exactly will run, and from where":
type (a bash builtin) — the most accurate: distinguishes alias,
function, builtin, and file.command -v — a portable way to learn the path/existence (for
scripts).which — a separate program, shows only the path to a file
(doesn't see aliases/builtins).whereis — binary + sources + man page.type -a and which -a show every match along $PATH, not just the
first one that would actually run — useful when a command behaves
unexpectedly and you suspect a version conflict. Plain which without
-a stops at the first file found and says nothing about aliases or
functions.
| Option | What it does | Example |
|---|---|---|
-a |
which — list every match in $PATH, not just the first one. | which -a python3 |
-a |
type — show every place the command was found, in search order. | type -a python3 |
-t |
type — print a single word (alias/keyword/function/builtin/file). | type -t cd |
-b |
whereis — search only for the binary, skipping man pages and sources. | whereis -b nginx |
-p |
type — print the file path only if it is an external command. | type -p python3 |
Nothing found. Try a shorter query.
A reliable "does this command exist" check in a script:
which found it, but the command says "not found" (or the reverse)which doesn't know about aliases/builtins/functions and reads its own
$PATH, which may differ from the current shell's. Trust type — it
asks the shell itself.
type -a name shows every match along PATH in search order — the first
one is what runs. Fix the order in $PATH or call it by full path.
command not found even though the package is installedThe binary isn't in $PATH (often /usr/local/sbin, ~/.local/bin).
Check echo $PATH and whereis name — see env-vars.
Bash caches the resolved path to a command in a hash table for the
session. Installing a new version alongside the old one (or through a
different package manager) doesn't reset this cache automatically: hash
-r makes bash search $PATH again, hash -t name shows what's
currently cached.
Found an inaccuracy? Report an error in the article