whence
Identify command's origin
TLDR
Interpret command, with expansion if defined as an alias (similar to the command -v builtin)
Display type of command, with location if defined as a function, or binary (equivalent to the type and command -V builtins)
Same as above, except display content of shell functions instead of location (equivalent to which builtin)
Same as above, but show all occurrences on command path (equivalent to the where builtin)
Search only the PATH for command, ignoring builtins, aliases or shell functions (equivalent to the where command)
SYNOPSIS
whence [-afpv] name [name ...]
PARAMETERS
-a
Show all occurrences of the name. For instance, if an alias exists and an executable with the same name also exists in the PATH, both will be reported.
-f
If the name refers to a shell function, display its definition rather than just indicating that it's a function. This is particularly useful for inspecting complex functions.
-p
Perform a search only using the command search path (PATH). This option bypasses checking for aliases, functions, or built-in commands, making it behave similarly to the which command.
-v
Produce a more verbose report for each name, providing more details about its type and location. This often adds extra descriptive text to the output.
DESCRIPTION
The whence command is a shell built-in utility, primarily found in KornShell (ksh) and Z Shell (zsh). Unlike external commands like which, whence provides a comprehensive lookup of how a command name will be interpreted by the shell. This includes identifying if the name refers to an alias, a shell function, a built-in command, or an external executable found in the PATH environment variable.
Its primary purpose is to help users and script writers understand the exact nature and location of a command, which is crucial for debugging shell scripts or understanding command precedence. For example, if you define an alias for 'ls', whence ls will tell you it's an alias, whereas which ls might only show the external executable.
CAVEATS
The whence command is a shell built-in and not an external executable. It is most commonly available in KornShell (ksh) and Z Shell (zsh). Users of Bash (Bourne-Again SHell) should use the type built-in command, which offers similar functionality. The exact behavior and available options for whence can vary slightly between different shell versions and implementations.
COMMAND PRECEDENCE
Shells resolve command names in a specific order: aliases first, then shell functions, then built-in commands, and finally external commands found in the PATH. whence helps illustrate this precedence by showing the *first* match the shell would use, and with -a, all potential matches.
DEBUGGING SHELL SCRIPTS
When debugging shell scripts, it's common to use whence to ensure that the script is executing the intended version of a command, especially when dealing with environments that have many custom aliases or functions.
HISTORY
The whence command originated in KornShell (ksh) as a powerful built-in for introspecting how commands are resolved by the shell. Its utility in distinguishing between aliases, functions, and external commands led to its adoption and similar implementations in other advanced shells like Z Shell (zsh), providing a more robust alternative to the simpler which utility.