13

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
n611x007
  • 957
  • 3
  • 11
  • 21
  • 3
    See also [Why not use “which”? What to use then?](http://unix.stackexchange.com/q/85249/22565) – Stéphane Chazelas Apr 23 '14 at 15:14
  • @StephaneChazelas that may mean my question is a duplicate. I've tried to search for this question but I missed what you linked. thanks! – n611x007 Apr 23 '14 at 16:00
  • well, the other question is perhaps properly described with that `all you never thought you would ever not want to know about it` - this leaves some room for mine . :) – n611x007 Apr 23 '14 at 20:51

3 Answers3

24

This should be a standard solution:

type
type -t
type -p
Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
  • you're right, it works, thank you, I forgot `type`. Is there a reference that declares it standard, by the way? like some document at opengroup. If there is, can you link it perhaps? – n611x007 Apr 23 '14 at 15:02
  • 7
    `type` and `command -V/v` are [standard (Unix, LSB, but optional in POSIX (XSI)](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/type.html), `type -p` and `type -t` are not. – Stéphane Chazelas Apr 23 '14 at 15:11
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,110
  • 17
  • 17
  • 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
2

whereis

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

Ghassan
  • 466
  • 2
  • 6