which() (was Re: Multiple executables in path)

Peter Chubb peterc at softway.sw.oz.au
Thu Jan 31 15:09:28 AEST 1991


My favourite solution, for the Bourne shell, at least, is:

#!/bin/sh
# which -- print full pathname of executable args.

for i
do
    IFS=":"
    for j in $PATH
    do
	test -x $j/$i && echo $j/$i
	exit 0
    done
done
exit 1

For shells with echo and test built in (most of them, now!), this involves 
no external executables.  By omitting the exit statements, all executables
with the given name(s) in the path are printed out.

Of course, it's trivial to convert this into a shell function, but if you 
do, make sure IFS is put back the way it's supposed to be!

Share and enjoy!


			Regards,

				- Peter Chubb

Softway Pty Ltd, P.O. Box 305, Strawberry Hills, NSW 2012, AUSTRALIA
Phone: +61 2 698 2322;       Fax: +61 2 699 9174;     Telex: AA27987
Internet: peterc at softway.oz.au	   UUCP: ...!uunet!softway.oz!peterc



More information about the Alt.sources.d mailing list