为什么将变量标记为只读会掩盖bash中的退出状态? [英] Why does marking a variable readonly mask the exit status in bash?

查看:58
本文介绍了为什么将变量标记为只读会掩盖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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆