ZSH: whence is helpful
There's been times where checking all elements of
$PATH for something is necessary – perhaps for debugging, overriding purposes, or otherwise.
One liners are handy for this – I'm certainly not one to shy away from a neato shell one-liner to accomplish the job (or use
find with some shell replacements). However! Code that you don't have to write is great in flow state (even if its a fun thought exercise that scratches an itch..) – ZSH lends itself to the task with a built in:
whence (its the same builtin behind
which). Check the manpage for the details, my copy (edited to fit nicely) says:
whence [ -vcwfpamsS ] [ -x num ] name ...
For each name, indicate how it would be interpreted if used as a command
If name is not an alias, built-in command, external command, shell
function, hashed command, or a reserved word, the exit status shall be
non-zero, and -- if -v, -c, or -w was passed -- a message will be written
to standard output. (This is different from other shells that write that
message to standard error.)
whence is most useful when name is only the last path component of a
command, i.e. does not include a `/'; in particular, pattern matching
only succeeds if just the non-directory component of the command is
-v Produce a more verbose report.
-c Print the results in a csh-like format. This takes precedence over -v.
-w For each name, print `name: word' where word is one of alias,
builtin, command, function, hashed, reserved or none, according as
name corresponds to an alias, a built-in command, an external
command, a shell function, a command defined with the hash
builtin, a reserved word, or is not recognised. This takes
precedence over -v and -c.
-f Causes the contents of a shell function to be displayed, which
would otherwise not happen unless the -c flag were used.
-p Do a path search for name even if it is an alias, reserved word,
shell function or builtin.
-a Do a search for all occurrences of name throughout the command
path. Normally only the first occurrence is printed.
-m The arguments are taken as patterns (pattern characters should be
quoted), and the information is displayed for each command
matching one of these patterns.
-s If a pathname contains symlinks, print the symlink-free pathname
-S As -s, but if the pathname had to be resolved by following
multiple symlinks, the intermediate steps are printed, too. The
symlink re‐ solved at each step might be anywhere in the path.
-x num Expand tabs when outputting shell functions using the -c option.
This has the same effect as the -x option to the functions builtin.
So the typical usage I'm looking for is:
# show builtin & the command
whence -a time
# giving this on NixOS (first being a builtin)
# or checking which `git` exectuables exist in (current, semi-contrived) PATH
whence -ap git
# giving this on Nix-on-NonNixOS host