How can I check the exit code of a command substitution in bash if the assignment is to a local variable in a function?
Please see the following examples. The second one is where I want to check the exit code.
Does someone have a good work-around or correct solution for this?
$ function testing { test="$(return 1)"; echo $?; }; testing
1
$ function testing { local test="$(return 1)"; echo $?; }; testing
0
If you look at the man file for
local(which is actually just the BASH builtins man page), it is treated as its own command, which gives an exit code of0upon successfully creating the local variable. Solocalis overwriting the last-executed error code.Try this:
EDIT: I went ahead and tried it for you, and it works.