The reason the code with local
returns 0 is because $?
“Expands to the exit status of the most recently executed foreground pipeline.” Thus $?
is returning the success of local
You can fix this behavior by separating the declaration of x
from the initialization of x
like so:
$ fun() { local x; x=$(false); echo "exit code: $?"; }; fun
exit code: 1