Bug in #! processing (long reply)

Garance A Drosihn drosih at rpi.edu
Sat Oct 2 11:59:27 PDT 2004

At 10:28 PM -0700 10/1/04, David G. Lawrence wrote:
>  > The following patch changes the check for end of line, so that
>  > a comment on the first line must be preceded by white space
>  > (' ' or '\t').
>    Personally, I'm thinking that all of the support for a "comment"
>on the interpreter line is bogus and it would be better to back out
>rev 1.21 rather than add even more complicated code to work around
>all of the problems that it causes.

 From some quick testing, it seems that # is not recognized as a
comment on Solaris, aix or recent redhat WS.  So I think we would
be better off not supporting it.

The way to support the documented feature is on a per-shell basis.
The command interpreter is passing the remaining arguments to the
shell it is calling, so any shell could decide that it will treat
"# <etc>" as a comment.  That means the behavior of "#" will depend
on which shell is being called, but then the behavior of everything
ELSE on that line is also determined by the shell being called, so
I don't see why comments should be a special-case.

The original change was based on

Which starts out by saying:

     Basically, if I follow the suggestions in the perl book
     to make portably executable scripts, I must use a shebang
     hack where the perl script starts being executed as a sh
     script. sh will pass it off to perl. Currently sh chokes
     on the # after -- as the executable.

I use this feature of perl myself, it is very useful in some
circumstances.  There is one important detail though -- the fix
which was committed is wrong on how the feature works.  The PR
gives the example of:

     #!/bin/sh -- # -*- perl -*-

     eval "exec perl $0 -S ${1+'$@'}"
       if 0;

     print "1+1=", (1+1), "\n";

First off, the above example script does not actually work on
linux (or at least not on Redhat WS 3.0).  Second, when it works
on solaris or aix, it works because of how /bin/sh processes the
rest of the line.  It is not because the command-interpreter has
stripped off everything after the '#'.  If you remove the '--' on
the above example, it fails immediately on aix and solaris.  In
fact, it seems to be the '--' which is causing the rest of the
line to be ignored, not the '#'.

The PR is correct in that this feature is documented by perl books,
but the right way to support the feature is to change /bin/sh and
not the command interpreter.  The PR includes a followup reply from
Ahmon Dancy <dancy at dancysoft.com> with some additional proof that
the change to the command interpreter was the wrong way to fix this.

Since this perl feature is documented in many places (including the
famous "Camel" book from O'Reilly), we can not just drop the support
in the command interpreter.  But if we first fix /bin/sh then I
believe we can drop it.

Garance Alistair Drosehn            =   gad at gilead.netel.rpi.edu
Senior Systems Programmer           or  gad at freebsd.org
Rensselaer Polytechnic Institute    or  drosih at rpi.edu

More information about the freebsd-current mailing list