which Command Cheat Sheet
which shows the full path of (shell) commands. It searches your $PATH.
Synopsis
which [options] command
Basic Usage
which python
# Output: /usr/bin/python
If it returns nothing, the command is not in your PATH.
Show All Matches (-a)
By default, it normally shows only the first match (the one that gets executed). To see duplicates/shadowed binaries:
which -a python
# Output:
# /usr/bin/python
# /bin/python
# /usr/local/bin/python
Validating Existence in Scripts
if which docker > /dev/null; then
echo "Docker is installed"
else
echo "Docker missing"
fi
command -v is preferred for portability).
Notes
- Aliases:
whichoften knows about aliases in interactive shells. - Builtins:
whichmight fail on shell builtins likecdorsource. Usetypeorcommand -vfor those.type cd # cd is a shell builtin