15

If the which command is not available, is there another 'standard' method to find out where a command's executable can be found?

If there is no other 'standard' method available, the actual system I face currently is a bare Android emulator with an ash Almquist shell, if that means anything.

n611x007
  • 1,007

3 Answers3

26

This should be a standard solution:

type
type -t
type -p
Hauke Laging
  • 90,279
3

whereis

Not quite the same, but should give you the binary's location like 'which' does.

Ghassan
  • 476
3

You can search the $PATH yourself to find a command:

COMMAND=vim # This is the command  to search for
(IFS=:; for dir in $PATH; do [ -x  $dir/$COMMAND ] && echo $dir/$COMMAND; done)

(this should work in ash and many other Bourne shell derivatives)

Johnny
  • 2,140
  • this is a good option if [ is included on the target system. – n611x007 Apr 23 '14 at 20:53
  • If your shell or keyboard doesn't support "[]" (I think ash does), you can replace [ -x $dir/$COMMAND ] with test -x $dir/$COMMAND (test is a bash/ash built-in, and may also be available as a standalone executable – Johnny Apr 23 '14 at 21:09
  • :) it's not actually my keyboard, my ash on this Android emulator seems not to come with either [ or test. I'm not sure if I'm doing anything wrong, the system seems stock and reports ash for shell. – n611x007 Apr 23 '14 at 21:22
  • 1
    I would regard any shell without either test or [...] as unusable for any but the most limited purposes. – iconoclast Nov 18 '14 at 00:04