The which command is a real life saver. It has become increasingly important in the last few years. Many vendors (like Sun) are providing separate directories of BSD-compatible and System V-compatible commands. Which command you'll get depends on your environment variable. It's often essential to know which version you're using. For example:
tells me exactly which version of the sort program I'm using. (Under SunOS 4.1, this is the BSD-compatible version in /bin , not the System V version in /usr/5bin .)
You'll find that which comes in handy in lots of other situations. I find that I'm always using which inside of backquotes to get a precise path. For example, when I was writing these articles, I started wondering whether or not man , apropos , and whatis were really the same executable. It's a simple question, but one I had never bothered to think about. There's one good way to find out:
What does this tell us? First, the three commands have the same file size, which means that they're likely to be identical; furthermore, each file has three links, meaning that each file has three names. The -i option confirms it; all three files have the same. So, apropos , man , and whatis are just one executable file that has three hard links.
Let's look at the setup, then explain it. For the C shell, use the following line in your .cshrc file:
(There's a similar shell function on the CD-ROM.) For this example, let's say you've also defined an alias for sort that looks like:
alias sort /usr/local/bin/quicksort
Okay. To run which , you type:
How did that work? The C shell runs the alias you defined for which . In this example, that executes the following command:
alias sort | /usr/local/bin/which -i sort
The first part of that command,
What if you ask which to find a command that you haven't aliased?
The shell runs this command:
alias tr | /usr/local/bin/which -i tr
Because there's no alias for
, the shell command
Nice trick, isn't it? Maarten Litmaath, the program's author, is a clever guy.
That's not all the new
option, it shows any alias you name and
searches your path for
commands with that name.
This is useful when you want to know all available versions of the command.
Let's end this article with an example from the manual page.
The first command shows all aliases (in this case, that's just the
alias for the new
Second, we run the new
to find which