使用局部变量时,返回代码($?)将重置为0而不是预期的4(请参阅打印值$ rc2)。这是打算,还是Bash中的错误?
我正在运行Debian:
$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)
重现的例子:
function foo(){
echo "Foo text [$1] [$2]"
return 4
}
echo "Testing function values and return values"
result=$(foo bar 20)
retVal=$?
echo "outside $retVal"
echo "outside $result"
function bar(){
output1=$(foo bar 20)
rc1=$?
local output2=$(foo bar 20)
rc2=$?
echo "inside $rc1 and $rc2"
echo "inside $output1 and $output2"
}
bar
产生以下输出:
outside 4
outside Foo text [bar] [20]
inside 4 and 0
inside Foo text [bar] [20] and Foo text [bar] [20]