home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Unix Power ToolsUnix Power ToolsSearch this book

2.6. Which Version Am I Using?

Your system may have several versions of a particular command -- for instance, a BSD-compatible version in one directory and a System V-compatible version somewhere else (and you might have added a private version in your own bin directory (Section 7.4)). Which command you'll get depends on your PATH (Section 35.6) environment variable. It's often essential to know which version you're using. For example:

$ type sort
sort is /bin/sort

tells me exactly which version of the sort program I'm using. (On one system I've used, there were two sorts; I had also defined an alias for sort.) If I want to see all versions, bash supports a -all option:

$ type -all sort
sort is aliased to `TMPDIR=/var/tmp /bin/sort'
sort is /bin/sort
sort is /usr/5bin/sort

A similar command is whence.

But type and whence are built into shells and are also Unix-version dependent (not all Unix systems have them), so they won't work everywhere. The which command is usually external (Section 1.9), so it works everywhere -- although, because it isn't built into the shells, it can't always find out about aliases defined in your current shell. For example:

% which sort

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. (whence and type may print extra text.) 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:

% ls -li `which man` `which apropos` `which whatis`
102352 -rwxr-xr-x  3 root        24576 Feb  8  2001 /usr/ucb/apropos
102352 -rwxr-xr-x  3 root        24576 Feb  8  2001 /usr/ucb/man
102352 -rwxr-xr-x  3 root        24576 Feb  8  2001 /usr/ucb/whatis

What does this tell us? Well, within this system 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 i-number. So, apropos, man, and whatis are just one executable file that has three hard links.

However, running the same command in another environment, such as in Darwin, results in a different output:

117804 -r-xr-xr-x  1  root    wheel    14332  sep  2  2001   /usr/bin/apropos
117807 -r-xr-xr-x  1  root    wheel    19020  sep  2  2001   /usr/bin/man
117808 -r-xr-xr-x  1  root    wheel    14336  sep  2  2001   /usr/bin/whatis

In Darwin, the commands are separate entities.

A few System V implementations don't have a which command.

--ML, JP, MAL, and SP

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.