I found out today while developing a shell script that which is not a builtin for sh. What is a good equivalent?


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

command is standardised here.

| improve this answer | |
  • 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 9 '15 at 17:06
  • 1
    Both type and command -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 9 '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 9 '15 at 17:13
  • No, and you're quoting an "informative", not spec section. – Stéphane Chazelas Feb 9 '15 at 17:13
  • 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 9 '15 at 17:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.