Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)

Ian Smith smithi at nimnet.asn.au
Mon Jul 20 03:17:06 UTC 2009


On Mon, 20 Jul 2009, Mark Andrews wrote:
 > In message <4ad871310907191717g1ed90be7y92250f2addc38d43 at mail.gmail.com>, Glen 
 > Barber writes:
 > > Possibly off-topic...
[..]
 > > > My understanding was this:
 > > >
 > > > If you specify 'sh foo.sh' at the shell, the script will be run in a
 > > > /bin/sh shell, _unless_ you override the shell _in_ the script.
 > > >
 > > > Ie, 'sh foo.sh' containing '#!/bin/sh' being redundant, but 'zsh
 > > > foo.sh' containing '#!/bin/sh' would execute using zsh.
 > > >
 > > >
 > > 
 > > I meant to say in the last line: "'#!/bin/sh' would override the 'zsh' shel=
 > > l."
 > > 
 > > Can someone enlighten me if I am wrong about this?
 >
 > "#!" is used to define the interpretor when the file is exec'd.
 > 
 > perl, AFAIK, is the only interpretor that will look at what is after
 > the "#!" and modify it's behaviour.  All other a interpretors (shells)
 > treat "#!" as a comment.
 > 
 > Some shells used to examine the executable about to be called and
 > looked for "#!" and invoke the correct interpretor.  This was how
 > "#!" was supported before kernels has support for "#!".  It was all
 > done in userland.

Some rexx scripts begin with this cute trick so they may be executed in 
any (UNIXish) shell as 'program', or specifically as 'rexx program', 
where it's just a regular rexx comment:

/*usr/bin/true;exec rexx -x "$0" "$@";exit# ReXX */
  /* Take a measure of REXX clauses-per-second (CPS)                 */
  /* Mike Cowlishaw (mfc at ibm.com).  Multi-platform.                  */
  /* 1.0 17 Jan 89 Original version                                  */

though I never understood why an exit would be needed after an exec .. 
just making sure I guess, or maybe catering for some variant or other.

cheers, Ian


More information about the freebsd-stable mailing list