How Unix didn't used to support '#!', a brief history
When I wrote about why an empty executable file is true in Unix, I mentioned that it's traditional Unix behavior that shell scripts without a '#!' line are passed directly to the Bourne shell. The short version of why this behavior exists is that before a certain point, Unix didn't understand '#!' lines at all.
In V7 (and versions before it), the exec*() family of system
calls only worked on actual binary executables. In order to make
shell scripts more or less work, the shell (and I think perhaps
some other programs) reacted to an ENOEXEC
error by assuming
the executable was actually a shell script. In the traditional
Unix manner, the shell didn't do any sort of safety check to see
if the file actually had ASCII text in it; if execve() returned
ENOEXEC
, that was good enough for it (you can see this in the
execs() function in sh/service.c).
(The V7 shell was actually reasonably clever about how it handled this case. Rather than exec'ing itself again, it longjmp()'d back to the start of main() in the same process.)
Before I started my research for writing this entry, I would have said that kernel support for '#!' was added in 4.2 BSD. That's certainly where it first appeared and where it became well known, but according to the history section of Wikipedia's page on #!, it was first introduced by Dennis Ritchie in 1980, after V7, although it might have been suggested to him by someone else. It didn't become known through later releases of Research Unix because those were never very widely spread, unlike V7.
(This suggests that at least some of my somewhat apocryphal history of comments in the Bourne shell is wrong. I'd guess that '#' was added as a comment character to the Bourne shell at some point after V7, since otherwise a shell script that started with '#!/bin/sh' would produce an error from the Bourne shell.)
Unix owes a lot to Dennis Ritchie as it is, but I'm still pleased and glad to learn that we owe a bit more to him than I thought (as well as to whoever suggested the idea to him). Plus, finding this out is a nice reminder to myself that the history of Unix can be more interesting than what I remember from the folklore I've absorbed over time.
|
|