为什么将变量标记为只读会掩盖bash中的退出状态? [英] Why does marking a variable readonly mask the exit status in bash?
本文介绍了为什么将变量标记为只读会掩盖bash中的退出状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对以下输出感到困惑:
I'm confused by this output:
$ readonly foo=`exit 1`
$ echo $?
0
$ bar=`exit 1`
$ echo $?
1
为什么readonly
会有所作为?
推荐答案
readonly
是一个函数调用,在exit 1
子外壳程序完成之后输入.
readonly
is a function call and is entered after the exit 1
subshell has finished.
您的脚本现在返回readonly
的返回值.
Your script now returns the return value of readonly
.
保留返回代码语义的单行代码可能是
A one-liner that retains the return code semantics could be
foo=`exit 1` ; RC=$? ; readonly foo ; `exit $RC`
这篇关于为什么将变量标记为只读会掩盖bash中的退出状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文