I found out today while developing a shell script that which
is not a builtin for sh
. What is a good equivalent?
Asked
Active
Viewed 1,331 times
2

Braiam
- 35,991

Roni Choudhury
- 123
1 Answers
2
Use command -v
, which is required in POSIX sh since issue 7. Note that if the command is a shell builtin, you might not get the results you expect (the output will not be a path).
$ command -v init
/usr/bin/init
command
is standardised here.

Chris Down
- 125,559
- 25
- 270
- 266
-
1@mikeserv
type
is also required to be a builtin -- from the spec: "Since type must be aware of the contents of the current shell execution environment (such as the lists of commands, functions, and built-ins processed by hash), it is always provided as a shell regular built-in." – Chris Down Feb 09 '15 at 17:06 -
1Both
type
andcommand -v
are optional in POSIX. They are not guaranteed to be present in POSIX conformant shells, they are in Unix conformant shells. They're not required to be builtin but in practice, they would have to be. Unix requires there be a non-builtin one (of limited usefulness). Systems that provide with them usually implement them as shell scripts that invoke the corresponding builtin. – Stéphane Chazelas Feb 09 '15 at 17:08 -
@StéphaneChazelas Is "it is always" not equivalent to "it is required to" in POSIX terminology? I was under the impression that they are the same.
type
at least is listed as "always" a builtin. – Chris Down Feb 09 '15 at 17:13 -
-
@StéphaneChazelas Thanks for the clarification, I will update accordingly. – Chris Down Feb 09 '15 at 17:15
-
@mikeserv, thanks. Looks like I'm not up to date. That was from memory from an older spec it would seem. – Stéphane Chazelas Feb 09 '15 at 17:20
-
1@mikeserv While true, that depends on what you're trying to do with it. For all we know the OP is trying to get the absolute path to things in $PATH :-) – Chris Down Feb 09 '15 at 17:37
command -v
command – mikeserv Feb 09 '15 at 16:51