Linux which Command: Find Executables in PATH

The which command searches directories listed in PATH and prints a matching executable path. It is convenient for locating an external program, but shell aliases, functions, and builtins are better diagnosed with type or command -v.

This guide explains the basic syntax, key options, practical examples, related commands, common cautions, and frequently asked questions for which.

What Is the which Command?

A typical implementation checks PATH from left to right and reports the first executable file. However, which may be an external program or a shell-specific wrapper, so its options and treatment of aliases can vary. Its output is not always identical to what the current shell will execute.

Basic Syntax

which [OPTION]... COMMAND...

Options and edge cases can vary by implementation and version. Check which --help and man which on the target system before relying on implementation-specific behavior.

Basic which Usage

Find an executable path

The first matching executable in PATH is printed.

which python3

Check several commands

Provide multiple command names to inspect them in one invocation.

which sh awk sed

Show all PATH matches

Common implementations support -a for every matching executable in PATH order.

which -a python3

Limitations of Executable File Path Output

which shows the path of the executable file found in PATH. Depending on the system and shell, the same command may be located in different places.

which ls

Example output:

/usr/bin/ls

Shell aliases or functions may differ between the which output and the actual interpretation of the entered command. To check what the current shell will execute, use type ls or command -V ls.

Key Options

Option Purpose
-a, --all Print all PATH matches instead of only the first.
--skip-alias In some GNU which versions, skip alias handling.
--skip-functions In some GNU which versions, skip shell-function handling.
--version Identify the installed implementation and version.

Practical Examples

Inspect command priority

Compare all matches with the order of directories in PATH.

which -a python3
printf '%s\n' "$PATH"

Ask the shell what it resolves

These shell commands can identify aliases, functions, builtins, and external files.

type -a ls
command -V ls
command -v ls

Test availability in a script

For portable POSIX shell code, use the exit status of command -v rather than parsing which output.

if command -v curl >/dev/null 2>&1; then
  printf '%s\n' 'curl is available'
fi

which and Related Commands

Command Primary role Best fit
which Search PATH for external executables Quick path lookup
command -v Report shell command resolution Portable script checks
type Classify aliases, functions, builtins, and files Interactive diagnosis
whereis Search program-related locations Binaries, source, and manuals

Important Cautions

Aliases and functions may be missed

The shell can choose an alias or function before an external executable. Confirm with type.

Implementations differ

Do not assume optional flags are available in every environment.

A path is not a security verdict

Finding an executable does not verify its owner, permissions, integrity, or trustworthiness.

FAQ

Does no output mean the command is not installed?

It means no executable was found in PATH. The name could still be a shell builtin, function, or alias.

How do I show every matching path?

Use which -a COMMAND when the implementation supports it.

Should scripts use which?

For portable shell scripts, command -v is generally the better availability test.

How is whereis different?

Which searches PATH for executables; whereis searches known locations for binaries, source, and manual pages.

Related Resources