简体   繁体   English

Shell Script 中带有 Shebang (#!/bin/bash) 的“-xe”代表什么?

[英]What does "-xe" with Shebang (#!/bin/bash) in Shell Script stand for?

I have copied a shell script and it is pretty much simple but on the first line with shebang, the -xe flag is specified.我复制了一个 shell 脚本,它非常简单,但在 shebang 的第一行,指定了-xe标志。

#!/bin/bash -xe

Can anyone explain what does it do?谁能解释它的作用? I have searched but could not find any helping material.我已经搜索但找不到任何帮助材料。

This is adding shell options to the shebang:这是向 shebang 添加 shell 选项:

  • -x , -o xtrace : Trace display executed statements. -x , -o xtrace :跟踪显示已执行的语句。
  • -e , -o errexit : Exit on error, but inconsistently for legacy POSIX compliance. -e , -o errexit :出错时退出,但对于遗留 POSIX 合规性不一致。
#!/bin/bash -xe

This shebang setting combines two discouraged bad practices with their own set of issues:这种 shebang 设置结合了两种不鼓励的不良做法和它们自己的一系列问题:

  1. First bad practice is setting shell options in the shebang;第一个不好的做法是在 shebang 中设置 shell 选项; because it means they will be ignored when invoking the script directly with: bash script_name , rather than making the script file executable, and invoking ./script_name .因为这意味着在直接使用以下命令调用脚本时它们将被忽略: bash script_name ,而不是使脚本文件可执行并调用./script_name
    Options shall be set explicitly in code with set -x , or preferably use their more straightforward long name (example: set -o xtrace instead of set -x ).选项应使用set -x在代码中显式设置,或者最好使用它们更直接的长名称(例如: set -o xtrace而不是set -x )。
  2. Second bad practice is using the -e option or set -o errexit which is only there for legacy POSIX compliance, but acts so inconsistently in different situations;第二个不好的做法是使用-e选项或set -o errexit仅用于遗留 POSIX 合规性,但在不同情况下行为不一致; that it causes more bugs than it avoids.它导致的错误多于避免的错误。

Here are some references about those set -e , set -o errexit quirks and issues:以下是关于那些set -eset -o errexit怪癖和问题的一些参考资料:

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

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