Glen Barber
2009-Jul-20 00:17 UTC
Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)
Possibly off-topic... 2009/7/19 Glen Barber <glen.j.barber@gmail.com>:> 2009/7/19 Romain Tarti?re <romain@blogreen.org>: >> Hi Glen, >> >> On Sun, Jul 19, 2009 at 04:32:28PM -0400, Glen Barber wrote: >>> > % sh foo.sh >>> > % zsh foo.sh >>> > % bash foo.sh >>> What happens if you replace '#!/bin/sh' with '#!/usr/local/bin/zsh' ? >> >> This is not related to my problem since I am not running the script >> using ./foo.sh but directly using the proper shell. ?sh just behaves >> differently, that looks odd so I would like to know if it is a bug in sh >> or if there is no specification for this and the behaviour depends of >> the implementation of each shell, in which case I have to tweak the >> script I am porting to avoid this construct (passing $? as an argument >> for example). >> >> Romain >> > > 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' shell." Can someone enlighten me if I am wrong about this? -- Glen Barber
Mark Andrews
2009-Jul-20 01:02 UTC
Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)
In message <4ad871310907191717g1ed90be7y92250f2addc38d43@mail.gmail.com>, Glen Barber writes:> Possibly off-topic... > > > 2009/7/19 Glen Barber <glen.j.barber@gmail.com>: > > 2009/7/19 Romain Tarti=E8re <romain@blogreen.org>: > >> Hi Glen, > >> > >> On Sun, Jul 19, 2009 at 04:32:28PM -0400, Glen Barber wrote: > >>> > % sh foo.sh > >>> > % zsh foo.sh > >>> > % bash foo.sh > >>> What happens if you replace '#!/bin/sh' with '#!/usr/local/bin/zsh' ? > >> > >> This is not related to my problem since I am not running the script > >> using ./foo.sh but directly using the proper shell. =A0sh just behaves > >> differently, that looks odd so I would like to know if it is a bug in sh > >> or if there is no specification for this and the behaviour depends of > >> the implementation of each shell, in which case I have to tweak the > >> script I am porting to avoid this construct (passing $? as an argument > >> for example). > >> > >> Romain > >> > > > > 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? > > --=20 > Glen Barber > _______________________________________________ > freebsd-stable@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-stable > To unsubscribe, send any mail to "freebsd-stable-unsubscribe@freebsd.org""#!" 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. Mark -- Mark Andrews, ISC 1 Seymour St., Dundas Valley, NSW 2117, Australia PHONE: +61 2 9871 4742 INTERNET: marka@isc.org
Lowell Gilbert
2009-Jul-20 11:29 UTC
Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)
Glen Barber <glen.j.barber@gmail.com> writes:> Possibly off-topic... > > > 2009/7/19 Glen Barber <glen.j.barber@gmail.com>: >> 2009/7/19 Romain Tarti?re <romain@blogreen.org>: >>> Hi Glen, >>> >>> On Sun, Jul 19, 2009 at 04:32:28PM -0400, Glen Barber wrote: >>>> > % sh foo.sh >>>> > % zsh foo.sh >>>> > % bash foo.sh >>>> What happens if you replace '#!/bin/sh' with '#!/usr/local/bin/zsh' ? >>> >>> This is not related to my problem since I am not running the script >>> using ./foo.sh but directly using the proper shell. ?sh just behaves >>> differently, that looks odd so I would like to know if it is a bug in sh >>> or if there is no specification for this and the behaviour depends of >>> the implementation of each shell, in which case I have to tweak the >>> script I am porting to avoid this construct (passing $? as an argument >>> for example). >>> >>> Romain >>> >> >> 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' shell." > > Can someone enlighten me if I am wrong about this?The person to whom you were responding had it closer. The shell specified in the "#!" first line is only consulted if you run it as "./foo.sh". Otherwise, it's input to the shell that you started, and the line is only a comment.
Glen Barber
2009-Jul-20 19:19 UTC
Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)
On Mon, Jul 20, 2009 at 3:12 PM, Doug Barton<dougb@freebsd.org> wrote:> Glen Barber wrote: >> Possibly off-topic... > > It's very off topic for -stable. If you want to follow up on this, > please do so in -hackers. > >> I meant to say in the last line: "'#!/bin/sh' would override the 'zsh' shell." >> >> Can someone enlighten me if I am wrong about this? > > It's trivial to create an example to test it yourself, don't ask when > you can do. :) > > #!/bin/foo > echo yes > > $ ./foo > bash: ./foo: /bin/foo: bad interpreter: No such file or directory > > $ sh foo > yes > > $ bash foo > yes > > > Now it's your turn. :) >Hi, Doug. The test I was going to run was: --------------------- #!/bin/sh echo $SHELL --------------------- which I would execute from $PATH and from 'sh/csh/zsh foo.sh', but I haven't had much time to play around with it. I plan on having time later today after ${JOB}. -- Glen Barber
Doug Barton
2009-Jul-20 19:39 UTC
Shell execution ( [was] Re: Value of $? lost in the beginning of a function.)
Glen Barber wrote:> Possibly off-topic...It's very off topic for -stable. If you want to follow up on this, please do so in -hackers.> I meant to say in the last line: "'#!/bin/sh' would override the 'zsh' shell." > > Can someone enlighten me if I am wrong about this?It's trivial to create an example to test it yourself, don't ask when you can do. :) #!/bin/foo echo yes $ ./foo bash: ./foo: /bin/foo: bad interpreter: No such file or directory $ sh foo yes $ bash foo yes Now it's your turn. :) Doug -- This .signature sanitized for your protection