sh: "local" assignment from command loses exit status
Fabian Keil
freebsd-listen at fabiankeil.de
Fri Nov 7 13:59:59 UTC 2014
Eric van Gyzen <eric at vangyzen.net> wrote:
> On 11/06/2014 12:30, Fabian Keil wrote:
> > Eric van Gyzen <eric at vangyzen.net> wrote:
> >
> >> In sh, if I use a single statement to declare a local variable and
> >> assign the output of a command to it, the exit status of that command is
> >> lost. For example:
> >>
> >> should_return_false() {
> >> local var1=`false`
> >> }
> >>
> >> The function should return non-zero, but it returns zero.
> > The function should return the return code of the last command.
> > In your example, the last command is "local".
>
> Fair enough. What about errexit? The shell ran a command whose exit
> status was not tested, that status was failure, yet the shell did not exit.
That's unrelated to the "local", though. Compare:
fk at r500 ~ $sh -e -c 'true `false; echo "Not reached"`; echo Reached'
Reached
While it's not obvious from the man page[1], my assumption is that this
is intentional behaviour. The return code of the command substitution
subshell can't be checked in the parent shell, as $? belongs to the
command that gets the output as argument (in your case "local"),
thus counting this as an untested return code would be inconvenient.
Fabian
[1] It could be argued that the behaviour is documented as
"-e ... tends to behave in unexpected ways", though ...
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freebsd.org/pipermail/freebsd-current/attachments/20141107/eb2a6ab0/attachment.sig>
More information about the freebsd-current
mailing list