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