简体   繁体   English

bash -eu之间的区别。 和“ bash -e”

[英]difference between “bash -eu” . and “bash -e”

Apologies if this is a repost. 抱歉,如果这是转贴。 I did search (no luck) before posting this. 我没有发布之前搜索(没有运气)。

"bash -e" will error/fail if there is any error. 如果有任何错误,“ bash -e”将错误/失败。 Doesn't it include the "bash -u" condition? 它不包括“ bash -u”条件吗? If a parameter is not set won't a command using that parameter fail and caught by "bash -e" ? 如果未设置参数,使用该参数的命令不会失败并被“ bash -e”捕获吗?

Isn't "bash -eu" equal to "bash -e" in that case? 在这种情况下,“ bash -eu”是否不等于“ bash -e”?

No, bash -e ( bash started with the errexit shell option set) is not the same as bash -e -u ( bash started with both errexit and nounset set). 不, bash -e (以errexit shell选项集开头的bash )与bash -e -u (以errexitnounset bash )不同。

Example: 例:

$ bash -e -c 'echo "hello $string"'
hello
$ echo "$?"
0
$ bash -e -u -c 'echo "hello $string"'
bash: string: unbound variable
$ echo "$?"
1

Using an unset variable under only errexit is not an error, it just expands to an empty string. 仅在 errexit下使用unset变量不是错误,它只会扩展为空字符串。

Also: 也:

$ bash -u -c 'echo "hello $string"'
bash: string: unbound variable
$ echo "$?"
127

This shows a subtle difference between -e and -u . 这显示了-e-u之间的细微差别。 With only -u , bash exits with code 127, which translates into a "command not found" error. 仅使用-ubash退出并返回代码127,该代码转换为“找不到命令”错误。 With both -e and -u , bash exits with a more generic error code of 1. 如果同时使用-e-u ,则bash退出时会出现更通用的错误代码1。


These things holds true for the POSIX sh shell as well, although I don't believe that the 127 exit status is explicitly required for the last example. 尽管我不认为最后一个示例明确要求127退出状态,但这些对于POSIX sh shell也适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM