Value of $? lost in the beginning of a function.

Glen Barber glen.j.barber at gmail.com
Sun Jul 19 20:32:30 UTC 2009


2009/7/19 Romain Tartière <romain at blogreen.org>:
> Hi!
>
> Simple test case:
>
> ----8<----------------------
> #!/bin/sh
> foo()
> {
>  echo "\$?=$? \$1=$1"
> }
> false
> foo $?
> ----8<----------------------
>
> % sh foo.sh
> $?=0 $1=1
> % zsh foo.sh
> $?=1 $1=1
> % bash foo.sh
> $?=1 $1=1
>
> As you can see, the value of $? is « lost » when FreeBSD sh enters a
> function.  Is this supposed to behave this way?
>

Hi.

I'm no expert at shell scripting, but my first presumption is that
since you have '#!/bin/sh' at the beginning of the script, it is
creating a new subshell, and overwriting the value.  What happens if
you replace '#!/bin/sh' with '#!/usr/local/bin/zsh' ?

-- 
Glen Barber


More information about the freebsd-stable mailing list