LinuxCommandLibrary

which

Locate executable file in user's environment

TLDR

Search the PATH environment variable and display the location of any matching executables

$ which [executable]
copy

If there are multiple executables which match, display all
$ which [[-a|--all]] [executable]
copy

SYNOPSIS

which [-a] [--skip-dot] [--skip-tilde] [--tty-only] [--read-alias] [--read-functions] [--show-tilde] [--show-dot] [--version] [--help] command ...

PARAMETERS

-a, --all
    Print all matching pathnames of each command, not just the first.

--skip-dot
    Skip directories in PATH that start with a dot.

--skip-tilde
    Skip directories in PATH that start with a tilde.

--tty-only
    Stop processing options if not on a tty.

--read-alias
    Read aliases from standard input.

--read-functions
    Read shell functions from standard input.

--show-tilde
    Output a tilde when the command is found at a path under the user’s home directory.

--show-dot
    Output a dot if the command is found in a directory that begins with a dot.

--version
    Display version information and exit.

--help
    Display this help text and exit.

DESCRIPTION

The which command is a utility used to locate the executable file associated with a given command. It searches the directories listed in the user's PATH environment variable, returning the full pathname of the first matching executable. This is helpful for determining which version of a command is being used, especially when multiple versions might be installed or when commands are overridden by aliases or shell functions. It can resolve aliases and functions found in the environment. If a command is not found in any of the directories specified in PATH, which will return an error.

which only searches the PATH and does not attempt to locate commands that are not intended to be executed directly, like shell built-in commands. It can be a valuable tool for understanding the execution environment and diagnosing path-related issues.

CAVEATS

which relies on the PATH environment variable. If PATH is not correctly set, which may not locate the intended command. It does not locate shell built-in commands.

EXIT STATUS

The which command returns an exit status of 0 if all specified commands are found. If one or more commands are not found, the exit status is 1.

ENVIRONMENT VARIABLES

PATH: Determines the search path for executable files. It is colon-separated.

HISTORY

The which command has been a standard utility in Unix-like operating systems for a long time. It's purpose is to help users understand exactly what executable will be run when they type a specific command in the shell.
Over time, different implementations have appeared but the fundamental concept remains the same.

SEE ALSO

whereis(1), type(1)

Copied to clipboard