[comp.unix.shell] the #! notation

afsipmh@cidsv01.cid.aes.doe.CA (01/17/91)

 Could someone clear up something for me?
 - If i put #! /bin/csh on the first line of a script
   the c shell will run it. If I put #! /bin/sh the
   Bourne shell will etc. 
 This is all well and good BUT does it assume that I am
 starting out in the c shell i.e. is the #! being interpreted
 by the c shell. What if my default were Bourne or ksh?
-- 
Pat Hertel                 Canadian Meteorological Centre
Analyst/Programmer         2121 N. Service Rd.        
phertel@cmc.aes.doe.ca     Dorval,Quebec             
Environment Canada         CANADA           H9P1J3

rickert@mp.cs.niu.edu (Neil Rickert) (01/22/91)

In article <1991Jan16.162705.580@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
>
> Could someone clear up something for me?
> - If i put #! /bin/csh on the first line of a script
>   the c shell will run it. If I put #! /bin/sh the
>   Bourne shell will etc. 
> This is all well and good BUT does it assume that I am
> starting out in the c shell i.e. is the #! being interpreted
> by the c shell. What if my default were Bourne or ksh?

 If your system supports '#!', these symbols are interpreted by the kernel,
not by the shell.  A process 'exec's a new command.  The kernel checks the
'magic number' at the beginning of the file to ensure that this is an
executable binary for this processor.  If it discovers that the 'magic
number' is '#!' it reads the name of the interpreter program and loads that
instead of the original file, but with the original file name as an
argument.

 In other words '#!', if supported, will work equally from the Bourne shell,
or any other shell, or even from a C program you write yourself.

-- 
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  Neil W. Rickert, Computer Science               <rickert@cs.niu.edu>
  Northern Illinois Univ.
  DeKalb, IL 60115                                   +1-815-753-6940

wrp@PRC.Unisys.COM (William R. Pringle) (01/22/91)

Incidentally, the entire line (except for the #!) is passed to the "shell"
specified.  This means that you can pass arguments to the shell.  It also
means that you can add the following line to the top of an awk script:

#!/bin/awk -f

Since # is a comment for awk, you can make this file executable and run
it directly from the shell, or specify is as a script file:

awk -f foobar.awk

OR:

foobar.awk

Bill Pringle
wrp@prc.unisys.com

dag@fciva.FRANKCAP.COM (Daniel A. Graifer) (01/22/91)

Perhaps I've missed something here, but none of this #! stuff works on my
machine (running Sys V/386 3.1).  Is this something that only works in 
BSD-derived unixes?  What about V3.2 or V4?

Appologies in advance if I've missed the answer previously in this thread/
newsgroup.

Dan
-- 
Daniel A. Graifer			Coastal Capital Funding Corp.
Sr. Vice President, Financial Systems	7900 Westpark Dr. Suite A-130
(703)821-3244				McLean, VA  22102
uunet!fciva!dag				fciva.FRANKCAP.COM!dag@uunet.uu.net

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (01/23/91)

In article <1991Jan16.162705.580@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
| 
|  Could someone clear up something for me?
|  - If i put #! /bin/csh on the first line of a script

  This is normally done by the kernel (if at all). It seems to me
there's a mechanism in SysV which put a line like

	: uses /bin/csh

as the first line, but I can't remember where I saw it. At any rate the
#! notation went into V.4, so it's now official.
-- 
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
    sysop *IX BBS and Public Access UNIX
    moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (01/23/91)

As quoted from <587@fciva.FRANKCAP.COM> by dag@fciva.FRANKCAP.COM (Daniel A. Graifer):
+---------------
| Perhaps I've missed something here, but none of this #! stuff works on my
| machine (running Sys V/386 3.1).  Is this something that only works in 
| BSD-derived unixes?  What about V3.2 or V4?
+---------------

It's a BSD-ism (and requires some kernel hacks to implement).  It also
includes the seeds of a security hole --- if you've ever read any messages on
the net about "disabling setuid shell scripts", the #! mechanism is both what
makes setuid shell scripts possible and what makes them insecure.

Note that some shells can emulate this mechanism when the kernel doesn't
support it --- I think bash does, for instance, and I have posted my "bexec"
mechanism for making any System V csh do so many times.  (I will *not* repost
it here yet again; if anyone wants it, send me mail.  But considering the
number of times I've posted it in the past, it ought to be archived all over
the net by now.)

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY

clewis@ferret.ocunix.on.ca (Chris Lewis) (01/24/91)

In article <1991Jan23.044019.4807@NCoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) writes:
|As quoted from <587@fciva.FRANKCAP.COM> by dag@fciva.FRANKCAP.COM (Daniel A. Graifer):
|+---------------
|| Perhaps I've missed something here, but none of this #! stuff works on my
|| machine (running Sys V/386 3.1).  Is this something that only works in 
|| BSD-derived unixes?  What about V3.2 or V4?
|+---------------

|It's a BSD-ism (and requires some kernel hacks to implement).

|Note that some shells can emulate this mechanism when the kernel doesn't
|support it --- I think bash does, for instance, and I have posted my "bexec"
|mechanism for making any System V csh do so many times.

Certain "stock" C-shells do it too.  I believe that BSD C shell manages
to recognize the "#" versus non-"#" prolog and attempts to do the right
thing as a short-cut.  Other C-shells too, for example, some flavours
of Xenix do it in the C-shell because there are no hooks in the kernel.
-- 
Chris Lewis, Phone: (613) 832-0541, Internet: clewis@ferret.ocunix.on.ca
UUCP: uunet!mitel!cunews!latour!ecicrl!clewis
Moderator of the Ferret Mailing List (ferret-request@eci386)

andy@xwkg.Icom.Com (Andrew H. Marrinson) (01/28/91)

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) writes:

>  It seems to me
>there's a mechanism in SysV which put a line like

>	: uses /bin/csh

>as the first line, but I can't remember where I saw it.

Just to clear up a common misconception here.  I know of no shell that
allows anything resembling ``: use some_program'' to specify an
arbitrary interpreter like the #! syntax does.  The idea that this
works seems to have come from Larry Wall's configure script, which
checks to see if #!/bin/sh arranges for scripts to be executed by
Bourne shell, and if that fails, checks : use /bin/sh.  The latter is
used because some older cshs (especially on system V and similar
Unixes that lack #!) treat scripts beginning with # as csh scripts
while scripts beginning with any other character are executed with
/bin/sh.  It is the ``:'' that is significant in this case.  The
phrase ``use /bin/sh'' is just a comment.
--
		Andrew H. Marrinson
		Icom Systems, Inc.
		Wheeling, IL, USA
		(andy@icom.icom.com)