[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: use local and $?
From: |
Eric Blake |
Subject: |
Re: use local and $? |
Date: |
Mon, 12 Jan 2015 09:31:08 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 |
On 01/12/2015 02:23 AM, l_j_f wrote:
> local b=$(error); err=$?
> echo b=$b err=$err
> }
> main "$@"
>
> 3. the result
> -sh-4.3# ./test2.sh
> a=ok err=0
> b=error err=0 #I think it should be "b=error err=1"
The code is behaving correctly, and only your expectations are wrong.
$() can only affect $? when it occurs in variable assignments in
isolation, but you used it as part of a 'local' command. 'local' is
documented as having exit status of 0 if it successfully assigned all
variables, and non-zero if it failed to set at least one variable.
Since b was successfully set, 'local' sets $? to 0, which overrides any
status in the embedded $().
You meant to write:
local b
b=$(error); err=$?
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature