Skip to content

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
(Though command -v is preferred for portability).


Notes

  • Aliases: which often knows about aliases in interactive shells.
  • Builtins: which might fail on shell builtins like cd or source. Use type or command -v for those.
    type cd
    # cd is a shell builtin