Why would you use the command 'which' in the terminal or shell?

Viewed 20

I'm starting to learn the terminal and shell and I'm not entirely understanding what the point of the 'which' command is. You can use it to find executables in PATH, but isn't the point of putting executables in PATH so you don't have to search for them? What other usage is there for which besides just verifying that they are in there?

1 Answers

This is useful when:

  • A command works in one terminal, but gives mycmd: command not found in another, so you want to figure out what's missing from your PATH
  • You want to figure out which of multiple versions will be run, e.g. will java run /usr/lib/jvm/java-11-openjdk-amd64/bin/java or /usr/lib/jvm/jdk-17/bin/java?
  • You want to determine whether you're invoking a user installed package in ~/.local/bin or a system-wide package in /usr/bin
  • You want to treat the executable as a file instead of just running it, such as cat "$(which ldd)"
  • You want its path to add it to a game launcher, IDE configuration, or similar

and undoubtedly a long tail of other scenarios

Related