16

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'
865k204 gold badges1.8k silver badges2.3k bronze badges
asked Apr 23, 2014 at 14:59
3
  • 3
    See also Why not use "which"? What to use then? Commented Apr 23, 2014 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! Commented Apr 23, 2014 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 . :) Commented Apr 23, 2014 at 20:51

3 Answers 3

28

This should be a standard solution:

type
type -t
type -p
answered Apr 23, 2014 at 15:01
2
  • 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? Commented Apr 23, 2014 at 15:02
  • 8
    type and command -V/v are standard (Unix, LSB, but optional in POSIX (XSI), type -p and type -t are not. Commented Apr 23, 2014 at 15:11
3

whereis

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

answered Apr 23, 2014 at 15:02
1
  • good to know, thanks, but it in the current OS it does not work for me Commented Apr 23, 2014 at 15:03
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)

answered Apr 23, 2014 at 20:43
4
  • this is a good option if [ is included on the target system. Commented Apr 23, 2014 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 Commented Apr 23, 2014 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. Commented Apr 23, 2014 at 21:22
  • 1
    I would regard any shell without either test or [...] as unusable for any but the most limited purposes. Commented Nov 18, 2014 at 0:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.