which
Locate executable file in user's environment
TLDR
Search the PATH environment variable and display the location of any matching executables
If there are multiple executables which match, display all
SYNOPSIS
which [options] command ...
command ...: One or more command names to locate.
PARAMETERS
-a, --all
Prints all matching executables found in the PATH, rather than just the first one.
-s, --silent
Suppresses output; only the exit status indicates success or failure. Useful for scripting.
-n, --no-alias, --no-functions
Instructs which not to search for shell aliases or shell functions. It only searches for executable programs.
DESCRIPTION
The which command is a utility on Unix-like operating systems used to locate the executable file associated with a given command. It performs its search by examining the directories listed in the user's PATH environment variable, in the order they appear. When a match is found, which prints the full path to the executable and then exits. If no match is found, it typically returns a non-zero exit status.
This command is particularly useful for determining which specific version of a command will be executed when multiple versions might exist in different PATH directories, or for identifying the physical location of an executable for scripting or debugging purposes. It primarily searches for external commands and typically does not identify shell built-in commands, functions, or aliases, which are handled by commands like type or command.
CAVEATS
which only searches for executable programs in the directories specified by the PATH environment variable. It typically does not find:
- Shell built-in commands (e.g., cd, echo in some shells).
- Shell functions defined within the current shell session.
- Shell aliases, unless the specific which implementation supports it (and usually needs to be explicitly enabled or is default, which is rare for the which utility itself, type is better for this).
- Executable files not located in a directory listed in PATH, including those in the current working directory (.) unless . is explicitly part of the PATH.
EXIT STATUS
The which command returns an exit status of 0 if all specified commands were found, and a non-zero status (typically 1) if at least one command was not found. This makes it suitable for use in shell scripts to check for the presence of commands.
PATH ENVIRONMENT VARIABLE
which is entirely dependent on the PATH environment variable. This variable contains a colon-separated list of directories where the shell looks for executable commands. The order of directories in PATH determines the precedence; which reports the first executable found in the search order.
HISTORY
The which command originated in the C shell (csh) and was later adopted as a standalone utility due to its widespread utility in determining the exact executable invoked by a given command name. Its primary purpose has always been to provide a clear path to an executable based on the shell's PATH search mechanism. Over time, different implementations have emerged, some with additional options (like GNU which) to handle more complex scenarios or provide more detailed control over the search, though its core functionality remains consistent across systems.