which - macOS
Overview
which – find the full path of a command
Syntax
which [-a] [-s] [-S] [-f] command ...
The which
command searches the system’s $PATH
environment variable for the first occurrence of each command specified on the command line and prints the full path of the command found.
Options/Flags
-a
: Display all matching commands, instead of just the first one found.-s
: Don’t display the matching commands; exit with a status of 0 if any of the commands are found in the$PATH
, and 1 otherwise.-S
: Silently ignore errors when searching for commands.-f
: Ignore alias and shell functions when searching for commands.
Examples
- To find the full path of the
ls
command:
which ls
- To find all matching occurrences of the
ls
command, including aliases and shell functions:
which -a ls
- To check if the
ls
command is in the$PATH
, without printing its path:
which -s ls
Common Issues
- If
which
cannot find a command, it will exit with a non-zero status. You can use the-S
option to ignore this error. - If you get an “ambiguous” error message, it means that more than one command with the same name was found in the
$PATH
. You can use the-a
option to see all matching commands.
Integration
The which
command can be used in conjunction with other macOS commands to perform advanced tasks. For example, you could use it to create a custom command that adds a specific directory to the $PATH
.
Related Commands
pathchk
man
help