WHICH(1)

๐ŸŒธ WHICH(1) โ€” like, the manual page for which, bestie ๐Ÿ’…

okay so real talk: this is the WHICH(1) page from the General Commands Manual, and it's genuinely one of the most underrated little tools ever. no cap. let's get into it ๐Ÿซถ


โœจ NAME โœจ

which โ€” shows the full path of (shell) commands. that's it. that's the vibe. ๐Ÿ‘‘

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

which [options] [--] programname [...]

๐ŸŽ€ DESCRIPTION ๐ŸŽ€

So which takes one or more arguments, right? For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. like, it's the "where does this even live" detective ๐Ÿ”

It does this by searching for an executable or script in the directories listed in the environment variable PATH, using the same algorithm as bash(1). same algorithm, icon behavior. ๐Ÿ’–

little meta moment: this man page is generated from the file which.texinfo. yes bestie, the docs have docs.

โœจ OPTIONS โœจ

  • --all, -a โ€” print all matching executables in PATH, not just the first. giving "i want the whole roster" energy ๐ŸŒˆ
  • --read-alias, -i โ€” read aliases from stdin, reporting matching ones on stdout. this is useful in combination with using an alias for which itself. for example
alias which=โ€™alias | which -iโ€™.
  • --skip-alias โ€” ignore option --read-alias, if any. this is useful to explicitly search for normal binaries, while using the --read-alias option in an alias or function for which. ๐Ÿ’€
  • --read-functions โ€” read shell function definitions from stdin, reporting matching ones on stdout. this is useful in combination with using a shell function for which itself. for example:
which() { declare -f | which --read-functions "$@" }
export -f which
  • --skip-functions โ€” ignore option --read-functions, if any. this is useful to explicitly search for normal binaries, while using the --read-functions option in an alias or function for which. ๐Ÿ™ˆ
  • --skip-dot โ€” skip directories in PATH that start with a dot. ๐Ÿšซ
  • --skip-tilde โ€” skip directories in PATH that start with a tilde and executables which reside in the HOME directory. ๐Ÿ 
  • --show-dot โ€” if a directory in PATH starts with a dot and a matching executable was found for that path, then print ./programname rather than the full path. cute lil shorthand โœจ
  • --show-tilde โ€” output a tilde when a directory matches the HOME directory. โš ๏ธ heads up: this option is ignored when which is invoked as root. sorry root girlies.
  • --tty-only โ€” stop processing options on the right if not on tty. ๐Ÿ›‘
  • --version, -v, -V โ€” print version information on standard output then exit successfully. ๐Ÿ’œ
  • --help โ€” print usage information on standard output then exit successfully. the classic ๐ŸŽ€

๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

which returns the number of failed arguments, or -1 when no programname was given. so like, the exit code is literally telling you how many of your little guesses flopped. it's giving report card. ๐Ÿ˜ญ


๐ŸŒธ EXAMPLE ๐ŸŒธ

The recommended way to use this utility is by adding an alias (C shell) or shell function (Bourne shell) for which like the following:

[ba]sh:

which ()
{
  (alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}
export -f which

[t]csh:

alias which โ€™alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tildeโ€™

This will print the readable ~/ and ./ when starting which from your prompt, while still printing the full path when used from a script:

> which q2
~/bin/q2
> echo $(which q2)
/home/carlo/bin/q2

the way that last one just works โ€” script mode gets the full path, prompt mode gets the cute version. main character energy fr fr ๐Ÿ’…


๐Ÿ’€ BUGS ๐Ÿ’€

okay, the not-so-slay parts:

The HOME directory is determined by looking for the HOME environment variable, which aborts when this variable doesn't exist. so if HOME ghosts you, which ghosts everyone. ๐Ÿ˜ญ

And which will consider two equivalent directories to be different when one of them contains a path with a symbolic link. lowkey rude but that's the behavior, no cap.


๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Carlo Wood <carlo@gnu.org> โ€” legend behavior, we stan ๐Ÿซถ

โญ SEE ALSO โญ


and that's the whole page bestie!! ๐Ÿ’– you just read the WHICH(1) manual page from the General Commands Manual โ€” no version number or date to be found, so consider this your cute little sign-off instead: go forth, find your binaries, and remember that HOME not existing will absolutely ruin your whole day. mwah ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from