Hacker News new | ask | show | jobs
by aidenn0 4252 days ago
Here's another POSIXism that everybody gets wrong: command lookup for sh; follow this algorithm for executing a command with no slashes, consider what would happen if "echo" is not in your path. Try it out on any shell and see that it just runs the "echo" builtin despite it being required to return 127.

Text below taken from: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...

If the command name matches the name of a special built-in utility, that special built-in utility shall be invoked.

If the command name matches the name of a function known to this shell, the function shall be invoked as described in Function Definition Command. If the implementation has provided a standard utility in the form of a function, it shall not be recognized at this point. It shall be invoked in conjunction with the path search in step 1d.

If the command name matches the name of a utility listed in the following table, that utility shall be invoked.

alias bg cd command

false fc fg getopts

jobs kill newgrp pwd

read true umask unalias

wait

Otherwise, the command shall be searched for using the PATH environment variable as described in XBD Environment Variables :

If the search is successful:

<snip>

If the search is unsuccessful, the command shall fail with an exit status of 127 and the shell shall write an error message.

2 comments

Quoting from your post:

"If the command name matches the name of a special built-in utility, that special built-in utility shall be invoked."

Let echo be a "special built-in utility", and sh is compliant.

"special built-in utilities" are specifically listed elsewhere. echo is not among them:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...

A lawyerly reading still permits echo to qualify as a special built-in utility.

Two relevant excerpts: "The following "special built-in" utilities shall be supported in the shell command language ..."

"... The term "built-in" implies that the shell can execute the utility directly and does not need to search for it. An implementation may choose to make any utility a built-in; however, the special built-in utilities described here differ from regular built-in utilities ..."

(the two differences are irrelevant to this discussion as they do not mention PATH resolution behaviors)

These quotes taken together do not preclude the shell adding a built-in utility and assigning it the "special" status. The standard does not say that only the listed utilities are special; it says that the listed utilities shall be special. Adding a normal built-in utility (not "special") not on the list is explicitly condoned.

It is my belief that echo being a "special built-in utility" and thus having magic PATH behavior is POSIX compliant, although the standard is not 100% clear on this.

Step 1.d.i.a:

If the system has implemented the utility as a regular built-in or as a shell function, it shall be invoked at this point in the path search.

1.d.i only happens if the search in PATH is successful.

I think the logic behind this was to ensure that as shells add more built-ins scripts that depend on an executable failing when not in the path won't break.