Workaround for the different behaviour of the which command in Solaris and in Linux
by Bernd Schemmer November 2009
In Linux you can use the which command like this:
With /bin/sh from Solaris this code does not work because the which command in Solaris always returns 0.
In a kornshell script (using /usr/bin/ksh) you can use the builtin command whence to get around this limitation, e.g.
But unfortunately whence is an internal command of ksh therfore it is not available for scripts executed by /bin/sh.
To get around this limitation the following code can be used in shell scripts for /bin/sh that should work in Solaris and in Linux:
Using this code you can use which in your shell scripts and it will work in Linux and in Solaris.
The shell /usr/xpg4/bin/sh in Solaris is a standards compliant
shell which provides all the functionality of ksh.
But in most cases you use "#/bin/sh" in shell scripts written for various Unix Operating systems and therefore the features of /usr/xpg4/bin/sh are not available.
In OpenSolaris the which command does return a value not equal
zero if the command is not found.