When using local variables, the return code ($?) is being reset to 0 instead of the expected 4 (see the printed value of $rc2). Is this intended, or is this a bug in Bash?
I am running Debian:
$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)
Example to reproduce:
function foo(){
echo "Foo text [$1] [$2]"
return 4
}
echo "Testing function values and return values"
result=$(foo bar 20)
retVal=$?
echo "outside $retVal"
echo "outside $result"
function bar(){
output1=$(foo bar 20)
rc1=$?
local output2=$(foo bar 20)
rc2=$?
echo "inside $rc1 and $rc2"
echo "inside $output1 and $output2"
}
bar
results in the following output:
outside 4
outside Foo text [bar] [20]
inside 4 and 0
inside Foo text [bar] [20] and Foo text [bar] [20]