WHICH(1)

πŸ’– WHICH(1) β€” the "where does this command even live" page πŸ’–

okay bestie, welcome to the General Commands Manual entry for which, and no, I'm not gonna be normal about it. this is the little utility that answers the question every girl asks at 2am: where is this executable actually coming from.


✨ NAME ✨

which β€” shows the full path of (shell) commands. that's it. that's the whole job. and she ATE. πŸ’…


🌸 SYNOPSIS 🌸

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

πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea, bestie. which takes one or more arguments. for each of its arguments, it prints to stdout the full path of the executables that would have been executed if you'd typed that argument at your shell prompt. like, it's doing a little detective work on your behalf and honestly? iconic.

how does she do it? she searches for an executable or script in the directories listed in the environment variable PATH, using the same algorithm as bash(1). same brain, different font. no cap.

also just so you know: this man page is generated from the file which.texinfo. nerdy. love that for her. πŸ’œ

πŸŽ€ OPTIONS πŸŽ€

  • --all, -a β€” print all the matching executables in PATH, not just the first one. she's showing you the whole group chat, not just the first person who replied. πŸ‘‘
  • --read-alias, -i β€” read aliases from stdin, and report the matching ones on stdout. super useful when you've got an alias for which itself. like this:
alias which=Β΄alias | which -iΒ΄.

yep, which looking up which. she contains multitudes. 🫢

  • --skip-alias β€” ignore the --read-alias option, if any is hanging around. useful when you wanna explicitly search for normal binaries while you're using --read-alias in an alias or function for which. basically: "not the aliases, bestie, the real ones." 🚫
  • --read-functions β€” read shell function definitions from stdin, and report the matching ones on stdout. great combo move with a shell function for which itself. for example:
which() { declare -f | which --read-functions $@ }
export -f which
  • --skip-functions β€” ignore the --read-functions option, if any. useful to explicitly search for normal binaries while using --read-functions in an alias or function for which. functions, we love you, but sit this one out. πŸ™ˆ
  • --skip-dot β€” skip directories in PATH that start with a dot. bye dot dirs, it's not personal. ✨
  • --skip-tilde β€” skip directories in PATH that start with a tilde, and skip executables that live in the HOME directory. no ~ energy today. 🌸
  • --show-dot β€” if a directory in PATH starts with a dot and a matching executable was found for that path, print ./programname instead of the full path. cute and short, we stan. πŸ’…
  • --show-tilde β€” output a tilde when a directory matches the HOME directory. ⚠️ heads up though: this option is ignored when which is invoked as root. root doesn't get the cozy ~ treatment, sorry. 😭
  • --tty-only β€” stop processing options on the right if you're not on a tty. tty vibe check: failed, we're done here. πŸ¦‹
  • --version, -v, -V β€” print version information on standard output, then exit successfully. three whole ways to ask "who are you" and she answers every time. πŸŽ€
  • --help β€” print usage information on standard output, then exit successfully. the classic. the icon. the "help me bestie" button. ⭐

πŸ”₯ RETURN VALUE πŸ”₯

which returns the number of failed arguments, or -1 when no programname was given. so if you gave her nothing, she's giving you -1 right back. valid honestly. πŸ’€


🌈 EXAMPLE 🌈

the recommended way to use this utility is by adding an alias (C shell) or a 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 you start which from your prompt, while still printing the full path when it's used from a script. best of both worlds, fr fr:

> which q2
~/bin/q2
> echo `which q2`
/home/carlo/bin/q2

see that?? main character at the prompt, professional in the script. obsessed. πŸ’–


πŸ’€ BUGS πŸ’€

okay so, the plot twist nobody asked for:

  • the HOME directory is determined by looking for the HOME environment variable, and it aborts when this variable doesn't exist. like, just gives up. no HOME, no service. 😭
  • which will consider two equivalent directories to be different when one of them contains a path with a symbolic link. so she's a little bit dramatic about symlinks. we forgive her. πŸ™ˆ

πŸ‘‘ AUTHOR πŸ‘‘

Carlo Wood <carlo@gnu.org>


🧚 SEE ALSO 🧚

bash(1)


and that's the whole page, bestie β€” WHICH(1) straight out of the General Commands Manual, and yes the footer really is just WHICH(1) again because she said her name twice for emphasis. periodt. πŸ’…βœ¨πŸ¦‹