2 shell questions before the new year

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Sat Dec 29 13:14:49 AEST 1984


> >   2. Why begin a shell script with
> >
> >		   #!/bin/sh
> >   or
> >		   #!/bin/csh
> 
> This tells the C shell which shell is supposed to interpret the
> shell script.  It's necessary because the syntax of shell
> commands is different in the two shells; I think /bin/sh is used
> by default.  However, if the script starts with a # and doesn't
> have the !  construct, the C shell is used

Let's get this right, please.  Recent BSD kernels understand the
"#!" as a "magic number" and exec() succeeds in executing such a
file, feeding it to the specified interpeter (which need not be one
of the "shells").  Only if an attempt to exec() fails will the shell
decide that it's a shell script and interpret it directly.  It is
extremely unfortunate that the Cshell thinks that # is not found in
Bourne shell scripts; apart from the #! line, practically all of
my Bourne shell scripts start with a # comment describing the function.

The full story of which shell will run your script was discussed
several months ago and is too gross to repeat.  Suffice it to say,
indicate with #!<shellpath> which shell you want and persuade your
Cshell to interpret #! lines if your kernel doesn't.



More information about the Comp.unix mailing list