bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: local keyword hides return code of command substitution


From: Eric Blake
Subject: Re: local keyword hides return code of command substitution
Date: Tue, 22 Sep 2015 09:44:31 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0

On 09/22/2015 08:19 AM, idallen@idallen-fibe.dyndns.org wrote:
> Description:
>       Adding a "local" keyword to a variable assignment hides the
>       return code of a command substitution.  Same problem in both
>       bash and dash shells.
> 

Not a bug.

$() substition can only affect $? if it is executed in isolation, and
not as an argument to some other command.  'local' is some other command.

And while 'local' is not (yet) in POSIX, the behavior is the same for
'export', which IS specified by POSIX.

For more details, read:
http://austingroupbugs.net/view.php?id=960#c2777

in particular the section that says:

> Note that, unless X was previously marked readonly, the value of $? after
> 
> export X=$(false)
> 
> will be 0 (because export successfully set X to the empty string) and that 
> execution continues, even if set -e is in effect. In order to detect command 
> substitution failures, a user must separate the assignment from the export, 
> as in
> 
> X=$(false)
> export X

So, as you discovered,

>       The work-around is to use "local" to declare the variable first,
>       then do the command substitution assignment on another line and
>       check the return code there.

That is not a workaround, so much as the correct behavior.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]