In a bash function, I'm trying to capture the output of a command along with it's exit code. Generally, my form is
some_function() {
local rv=$(some_other_function)
local code=$?
echo "$rv"
return $code
}
but I notice that whenever I use 'local', then $? is ALWAYS 0. Like it's capturing the result of the assignment, not the called function. If I don't use 'local', then it works as expected:
$ foo() { local rv=$(false); echo "code is $?"; }
$ foo
code is 0
$ foo() { rv=$(false); echo "code is $?"; }
$ foo
code is 1
Can someone explain this to me? Obviously something fundamental here I just don't understand.