WHICH(1) User Commands WHICH(1)

whichlocate a program file in the user's path

which [-as] command ...

The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.

Some shells may provide a built-in which command which is similar or identical to this utility.

The following options are available:

List all instances of executables found (instead of just the first one of each).
No output, just return 0 if all of the executables are found, or 1 if some were not found.

The following operands are supported:

command
The name of a command to be located.

See environ(7) for descriptions of the following environment variables that affect the execution of which: PATH.

The following exit values are returned:

All of the provided operands were found in the path and would be executed if the commands were invoked.
One or more of the provided operands was not found in the path or would not be executed if the commands were invoked, or an error occurred.
Invalid command line options were specified.

Example 1 Locate the ‘ls’ and ‘cp’ commands.

$ /usr/bin/which ls cp
/usr/bin/ls
/usr/bin/cp

Example 2 Use a specific PATH and show all occurrences.

$ PATH=/usr/bin:/usr/has/bin /usr/bin/which -a edit
/usr/bin/edit
/usr/has/bin/edit

Example 3 Executable reported multiple times.

If PATH contains repeated components, the same executable can be reported more than once:

$ PATH=/usr/bin:/usr/bin /usr/bin/which -a ls
/usr/bin/ls
/usr/bin/ls

Example 4 Do not show output.

Just exit with an appropriate return code:

$ /usr/bin/which -s ls cp
$ echo $?
0
$ /usr/bin/which -s fakecommand
$ echo $?
1

The command line interface of which is Committed. The output of which is Committed.

find(1), environ(7)

March 15, 2023 OmniOS