This example was tested on Mac El Capitan with bash
main_script.sh:
NOTE: func_a and func_b are identical except for the line that the local variable
output
is declared.
func_a () {
local output
output="$(./external.sh some_function)"
if [ $? -eq 0 ];then
echo "A zero result ($?) -> $output <- end"
else
echo "A other result ($?) -> $output <- end"
fi
}
func_b () {
local output="$(./external.sh some_function)"
if [ $? -eq 0 ];then
echo "B zero result ($?) -> $output <- end"
else
echo "B other result ($?) -> $output <- end"
fi
}
func_a
func_b
external.sh:
some_function () {
echo "this is the output"
return 1
}
"$@"
When i run main_script the output is:
A other result (1) -> this is the output <- end
B zero result (0) -> this is the output <- end
For what reason would the declaration of a local variable on the same line as the command substitiution affect the results? Could this be a bug or am i missing something?