Welcome to the Linux Foundation Forum!

bash function return value

why is this :
function f1() {
echo "f1"
return 21
}
function f2() {
local ans=$(f1)
declare -i rcode=$?

echo "<$rcode> <$ans>" >&2

echo "f2"
return $rcode
}
ans=$(f2)
echo "<$?> <$ans>"
prints :
<0>

but :
function f1() {
echo "f1"
return 21
}
function f2() {
local ans=""
ans=$(f1)
declare -i rcode=$?
# echo "<$?> <$ans>" >&2
echo "f2"
return $rcode
}
ans=$(f2)
echo "<$?> <$ans>"
prints :
<21>

Categories

Upcoming Training