Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux which command

Reference for the Linux which command: show which executable runs for a name, list every match with -a, and why command -v is more reliable in scripts.

which searches the directories in your PATH and prints the full path of the executable that would run for a given name. It answers "which python am I getting?" when several versions are installed.

which only looks at files on PATH. It knows nothing about shell builtins, aliases or functions, which is why type or command -v are better when the exact behavior matters.

Terminal
which [-a] COMMAND...

Try it

Bash Finding executables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash which versus type and command -v

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
which [-a] [--] PROGRAM...
Option Description
-a Print every match on PATH, not just the first.
-s Silent: print nothing, only set the exit status (some versions).

Returns: 0 if all names were found, 1 if at least one was not, 2 on invalid options (Debian's which).

Usage notes

  • Only the first match is the one that runs. The directory order of PATH decides. echo "$PATH" | tr ':' '\n' lists it in order.
  • Several implementations. Debian's which is a small shell script; the GNU which on Fedora can also read aliases (with --read-alias); BSD systems have their own. Options and messages differ.
  • Not for builtins, keywords, aliases or functions. which echo finds /bin/echo, but typing echo in bash runs the builtin.
  • In scripts use command -v name, which is POSIX, needs no external process and knows about builtins. which as an external program may be missing.
  • Hash table. Bash caches the locations of commands it ran. After installing a new version, run hash -r if the shell keeps using the old path.
  • Relative PATH entries (an empty entry or .) make which print relative paths. They also allow running programs from the current directory by accident; avoid them.
  • Symlinks are not resolved: which python may print a link. Use readlink -f "$(which python)" to see the real file.
  • Exit status makes which name >/dev/null usable in if, but command -v is the portable choice.