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.
which [-a] COMMAND...
Try it
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.
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
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
PATHdecides.echo "$PATH" | tr ':' '\n'lists it in order. - Several implementations. Debian's
whichis a small shell script; the GNUwhichon 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 echofinds/bin/echo, but typingechoin bash runs the builtin. - In scripts use
command -v name, which is POSIX, needs no external process and knows about builtins.whichas an external program may be missing. - Hash table. Bash caches the locations of commands it ran. After installing a new version, run
hash -rif the shell keeps using the old path. - Relative
PATHentries (an empty entry or.) makewhichprint relative paths. They also allow running programs from the current directory by accident; avoid them. - Symlinks are not resolved:
which pythonmay print a link. Usereadlink -f "$(which python)"to see the real file. - Exit status makes
which name >/dev/nullusable inif, butcommand -vis the portable choice.
Related
type,whereis,readlink,find- Lessons: Environment, Getting Help