Subject: Re: replacement for which(1)
To: Bill Studenmund <>
From: Simon J. Gerraty <>
List: tech-userlevel
Date: 04/01/2004 14:26:56
>That and built-ins are the two reasons I think we shouldn't try to use an=
>external command.

Yes and no.  Many scripts want to simply - and exactly, locate a binary
via $PATH or know that no such binary exists.

The existing which(1) is inappropriate due since its a csh script
and follows the wrong semantics for the above.

Shell builtins like type, whence etc, are also not very useful since every 
shell has a different one, and not all of them do just the above.

Another external command might be good - but is useless unless it is widely
available with the correct semantics.

The solution I've used for years is:


Which() {
	case "$1" in
	-*) t=$1; shift;;
	*) t=-x;;
	case "$1" in
	/*)	test $t $1 && echo $1;;
		for d in `IFS=:; echo ${2:-$PATH}`
			test $t $d/$1 && { echo $d/$1; break; }

case /$0. in
*/[wW]hich.*) Which "$@";;

Which you can place in a file ( and use as an external command
or . to get the function - depending on how many times you need to use it.