[net.unix] #! in shell scripts

fair@dual.UUCP (Erik E. Fair) (03/06/84)

I have it on good authority that the #! kernel hack for making shell
scripts directly executable is a 32Vism. Since 4BSD is a 32V derivative,
it has it. Since System III and V are not 32V derivatives, they don't.

For the users of 4BSD systems: If you want to write Bourne Shell scripts,
go ahead. But if you ever have the notion to port them to USG Systems, or
V7 Systems, your scripts will fail for all users of csh, which assumes
that a shell script with `#' at the beginning is a csh script, and not
a sh script. Better that you do something like this:

: This comment line will do the right thing for all versions of UNIX,
: if you like Bourne Shell Scripts.
PATH=/foo/bar:/snafu/bin:/etc:; export PATH

and so on, ad nauseum.

	Erik E. Fair

	dual!fair@BERKELEY.ARPA
	{ihnp4,ucbvax,cbosgd,decwrl,amd70,fortune,zehntel}!dual!fair
	Dual Systems Corporation, Berkeley, California

guy@rlgvax.UUCP (Guy Harris) (03/06/84)

> I have it on good authority that the #! kernel hack for making shell
> scripts directly executable is a 32Vism. Since 4BSD is a 32V derivative,
> it has it. Since System III and V are not 32V derivatives, they don't.

It is *definitely* a Bell Labs invention, but I suspect it wasn't in 32V
(not having the source, I can't check; anybody out there actually run 32V?)
because the paper "Changes in the Kernel in 4.1BSD/May 10, 1981" by Bill
Joy, which comes with 4.1BSD, says under "sys1.c":

	Implements the "#!" executable shell script scheme.

So I suspect the only publicly released version of UNIX which has it is
4.xBSD (and 2.xBSD if it has it).  Also, I suspect System III (VAX-11) is
a 32V derivative in the same sense that System III (PDP-11) is a V7 derivative;
the original work done by Reiser, London, et al was picked up by USG to
make the VAX-11 version of USG UNIX.  The reason that it appears in 4.xBSD
and not other versions is probably that Berkeley were the only people that
picked it up from the Labs ("style", "diction", EFL, SDB and symbolic links
are also Bell Labsisms but for quite a while the only way you could get the
first four was from 4.1BSD, and the latter only appears in 4.2BSD).

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy

jpl@allegra.UUCP (John P. Linderman) (03/06/84)

The convention that files beginning with "#!" are to be interpreted
by the command whose name follows is a useful and flexible one.
The convention that files beginning with "#" are csh scripts is
inflexible and demonstrably wrong.  It would be more productive to
fix csh (and any other shells that don't recognize the useful
convention) than to ask shell script authors to warp their scripts
to match a bad assumption.

John P. Linderman  Department of Shell Shock Trauma  allegra!jpl

jab@uokvax.UUCP (03/12/84)

#R:dual:-32600:uokvax:6100026:000:464
uokvax!jab    Mar  9 16:52:00 1984

One can't underestimate the power that the "#!" construct gives you.
You don't have to have a SHELL run the blame script.

For example, the following "exec-able shell script" tells you how many lines
are in the input:

	#!/bin/awk -f
	END { print NR }

Admittedly this is a simple example, but it hints on the flexibility this
gives you.

	Jeff Bowles
	Lisle, IL

ps. ":" is NOT a comment line -- it doesn't work predictably with the line
	: This (was) a comment

emjej@uokvax.UUCP (03/13/84)

#R:dual:-32600:uokvax:6100027:000:144
uokvax!emjej    Mar 10 00:40:00 1984

Sigh. It also hints at the bogosity of wiring magic numbers
into the kernel. How many command languages use # as a
comment flag?

		James Jones

ed@unisoft.UUCP (03/18/84)

It is my understanding that the #! magic number was suggested by
Dennis Ritchie and implemented by Dennis and/or Bill Joy.  In
any case, the idea seems to have come from Bell.

Its original motivation was not particularly for shell scripts,
however, but to deal with the output of "pi", which is a translator
from Pascal to an interpreted intermediate code.  To execute the
intermediate, "px" is used.  It seemed nice to be able to utter
just the name of the pi output file to get it executed, rather
than "px filename".  (There was actually a kludge put in to the
pi output files to make them executable, but it was pretty expensive
to start up as it involved an extra fork, exec, and pipe.)
Thus was born the "specified interpreter" magic number #!.
These characters were chosen for compatibility with existing
shell scripts.

Having the kernel recognize this extra magic number, as has been
pointed out by several others, is not particularly expensive,
since the kernel already reads the exec header from the file
to determine if it's executable at all.

-- 
Ed Gould
ucbvax!mtxinu!ed