[comp.unix.questions] #! /bin/sh line in a shell script

ghe@nucthy.physics.orst.edu (Guangliang He) (01/31/89)

I have seen number of times that a shell script file begining with a line
#! /bin/sh. What does that mean? 

I know that any shell script file begining with a # sign is treated as
a c-shell script and the following content of that line is treated as
comment.

Please email to me. Thanks in advance.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                                   *                                   
USMAIL:   Guangliang He            *   INTERNET: ghe@PHYSICS.ORST.EDU  
          Department of Physics    *                                   
          Oregon State University  *                                   
          Corvallis, OR 97331      *   PHONE:    (503) 754-4631        
                                   *                                   
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

matthew@sunpix.UUCP ( Sun NCAA) (02/02/89)

In article <8575@orstcs.CS.ORST.EDU>, ghe@nucthy.physics.orst.edu (Guangliang He) writes:
> I have seen number of times that a shell script file begining with a line
> #! /bin/sh. What does that mean? 
> 
> I know that any shell script file begining with a # sign is treated as
> a c-shell script and the following content of that line is treated as
> comment.

    This little tidbit from a man page should help you figure it out.

     When a pathname is found that  has  proper  execute  permis-
     sions,  the  shell  forks a new process and passes it, along
     with its arguments to the kernel (using the execve(2) system
     call).   The kernel then attempts to overlay the new process
     with the desired program.  If  the  file  is  an  executable
     binary (in a.out(5), the kernel succeeds, and begins execut-
     ing the new process.  If the file is a text  file,  and  the
     first  line begins with #!, the next word is taken to be the
     pathname of a shell (or command) to interpret  that  script.
     Subsequent  words on the first line are taken as options for
     that shell.  The kernel  invokes  (overlays)  the  indicated
     shell, using the name of the script as an argument.


-- 
Matthew Lee Stier     (919) 469-8300|
Sun Microsystems ---  RTP, NC  27560|          "Wisconsin   Escapee"
uucp: {sun, rti}!sunpix!matthew     |

chris@mimsy.UUCP (Chris Torek) (02/06/89)

Time to re-post this, I suppose.

Path: maryland!umcp-cs!chris
From: chris@umcp-cs.UUCP (Chris Torek)
Newsgroups: net.unix-wizards
Subject: Re: # sign in shell/cshell scripts...
Message-ID: <995@umcp-cs.UUCP>
Date: 17 Apr 86 20:30:30 GMT
Date-Received: 17 Apr 86 20:30:30 GMT
References: <237@Shasta.ARPA>

In article <237@Shasta.ARPA> ALEX@SU-SIERRA.ARPA writes:
>	I'm encountering weird problems with sh (and sometimes csh)
>dying on some shell scripts depending on whether or not my first line in
>the script is a comment like:
># This script does this (but not that)

Once upon a time, there was the shell.  Since there was only `the'
shell, there was no trouble deciding how to run a script: run it
with *the* shell.  It worked, and everyone was happy.

Along came progress, and wrote another shell.  The people thought
this was good, for now they could choose their own shells.  So some
chose the one, and some the other, and they wrote shell scripts and
were happy.  But one day someone who used the `other' shell ran
a script by someone who use the `other other' shell, and alas! it
bombed spectacularly.  The people wailed and called upon their
Guru for help.

`Well,' said the Guru, `I see the problem.  The one shell and the
other are not compatible.  We must ensure that the shells know
which other shell to use to run each script.  And lo! the one shell
has a ``comment'' called ``:'', and the other a true comment called
``#''.  I hereby decree that henceforth, the one shell will run
scripts that start with ``:'', and the other those that start with
``#''.'  And it was so, and the people were happy.

But progress was not finished.  This time he noticed that only
shells ran scripts, and thought that if the kernel too could run
scripts, that this would be good, and the people would be happy.
So he wrote more code, and now the kernel could run scripts,
but only if they began with the magic incantation: `#!', and
told the kernel which shell ran the script.  And it was so, and
the people were confused.

For the `#!' looked like a `comment'.  Though the kernel could see
the `#!' and run a shell, it would not do so unless certain magic
bits were set.  And if the incantation were mispronounced, that
too could stop the kernel, which after all was not omniscient.
And so the people wailed, but alas!, the Guru did not respond.
And so it was, and still it is today.

			*   *   *   *   *

Anyway, you will get best results from a 4BSD machine by using

	#! /bin/sh

or

	#! /bin/csh

as the first line of your script.  `#! /bin/csh -f' is also helpful
on occasion.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu


-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

tjo@Fulcrum.BT.CO.UK (Tim Oldham) (02/09/89)

In article <382@greens.UUCP> matthew@sunpix.UUCP ( Sun NCAA) writes:
>In article <8575@orstcs.CS.ORST.EDU>, ghe@nucthy.physics.orst.edu (Guangliang He) writes:
>> I have seen number of times that a shell script file begining with a line
>> #! /bin/sh. What does that mean? 
>
>    [matthew explains about kernel handling of #! ]

This isn't valid for all kernels ie versions and flavours of Unix. There's a
problem here; some flavours use #! and some assume your $SHELL, unless the
first line is blank, in which case they use /bin/sh. Such is my limited
experience, anyway.

	-> Tim
-- 
Tim Oldham      ...!mcvax!ukc!axion!fulcrum!tjo  or  tjo@fulcrum.bt.co.uk
#include	<stdisclaim>
"After five years in the institution, now I wanna be a good boy" - The Ramones