2 shell questions before the new year

Ed Gould ed at mtxinu.UUCP
Mon Jan 7 14:26:09 AEST 1985


> >   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

On recent 4bsd systems (certainly 4.2, I believe 4.1 and don't remember
before) this construct also allows the file to be handled by the
kernel's exec() routine.  The documented syntax is

	#! pathname arg

where the space after the ! (exactly one space character, or perhaps
either one space or one tab) is required, and there may be no more than
one argument.  (In reality, the space has never to my knowledge been
required.)

The advantages of directly-execable scripts are two-fold.  First,
it allows *any* interpreter to be specified for the rest of the
file, not just a shell.  Second, the set-uid and set-gid bits
are honored.  Voila!  Set-uid shell scripts!

-- 
Ed Gould		    mt Xinu, 739 Allston Way, Berkeley, CA  94710  USA
{ucbvax,decvax}!mtxinu!ed   +1 415 644 0146
			    (I'd rather not be parochial.)



More information about the Comp.unix mailing list