which

Locate a command on PATH.

Source: src/scripting/command_builtin.f90:88-141

Synopsis

which [-s] command [command ...]

Description

Searches PATH for each named command and prints the full path of the executable that would be run. Similar to type but only searches external commands (not builtins, functions, or aliases).

Options

FlagDescription
-sSilent mode — don't print anything, just set exit status

Usage

which ls
# /usr/bin/ls

which fortsh
# /usr/local/bin/fortsh

which ls cat grep
# /usr/bin/ls
# /usr/bin/cat
# /usr/bin/grep

# Check if a command exists without output
if which gcc -s; then
    echo "GCC is installed"
fi

If a command name contains /, it is treated as an explicit path and checked directly for executability.

Exit Status

StatusCondition
0All commands found
1One or more commands not found
2No arguments given

See Also

  • type - Identify a command (includes builtins, functions, aliases)
  • command - Run a command bypassing functions
  • hash - Manage the command hash table