How to get the pathname of the current executable?

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Feb 15 02:32:16 AEST 1990


In article <1747 at skye.ed.ac.uk> richard at aiai.UUCP (Richard Tobin) writes:
>Different shells potentially interpret commands in completely different
>ways.  A command like which *has* to depend on your shell.  It seems clear
>to me that which should be built-in to csh and sh - that way it would
>always be right.

Well, without a precise definition of what it is that we expect "which"
to do, the issue cannot be settled.

My own view is that "which" and "every" should report ONLY on $PATH-based
commands (assuming standard UNIX, i.e., not multiple mounts on /bin), and
that "whatis" should be a builtin that produces a definition suitable for
feeding back to the shell (unlike System V's "type" builtin).

Here are some typical examples:

$ echo $PATH
/usr/lbin:/usr/5bin:/bin:/usr/bin:~/bin:/usr/ucb:.
$ whatis which
/usr/lbin/which
$ whatis every
/usr/lbin/every
$ whatis whatis		# 8th or 9th Edition UNIX or BRL Bourne shell
builtin whatis
$ whatis cd
builtin cd
$ whatis builtin	# 8th or 9th Edition UNIX or BRL Bourne shell
builtin builtin
$ whatis l
l () { ( set +u ; exec ls -bCF $* ) }
$ whatis sh
/usr/lbin/sh
$ whatis xyzzy
# xyzzy not found
$ which which
/usr/lbin/which
$ which every
/usr/lbin/every
$ which whatis
/usr/ucb/whatis
$ which cd
which: cd: not found
$ which builtin
which: builtin: not found
$ which l
which: l: not found
$ which sh
/usr/lbin/sh
$ which xyzzy
which: xyzzy: not found
$ every which
/usr/lbin/which
/usr/ucb/which
$ every every
/usr/lbin/every
$ every whatis
/usr/ucb/whatis
$ every cd
every: cd: not found
$ every builtin
every: builtin: not found
$ every l
every: l: not found
$ every xyzzy
every: xyzzy: not found
$ every sh
/usr/lbin/sh
/usr/5bin/sh
/bin/sh

(Actually, for interactive use I normally redefine "cd" and "which" using
shell functions, but the example is clearer if I show the default behavior.)



More information about the Comp.unix.questions mailing list