什么是“ -xe”是什么Shell脚本中的Shebang(#!/bin/bash)代表?

发布于 2025-02-13 20:04:19 字数 144 浏览 0 评论 0原文

我已经复制了一个shell脚本,这很简单,但是在Shebang的第一行中,指定了-Xe标志。

#!/bin/bash -xe

谁能解释它做什么?我已经搜索过,但找不到任何帮助材料。

I have copied a shell script and it is pretty much simple but on the first line with shebang, the -xe flag is specified.

#!/bin/bash -xe

Can anyone explain what does it do? I have searched but could not find any helping material.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2025-02-20 20:04:19

这是在Shebang中添加Shell选项:

  • -X-O Xtrace:跟踪显示执行的语句。
  • -e-o errexit:错误时退出,但不一致,但对于旧版POSIX的遵守情况而言不一致。
#!/bin/bash -xe

这种Shebang设置将两种灰心的做法与自己的一系列问题结合在一起:

  1. 第一个不良练习是在Shebang中设置外壳选项;因为这意味着当直接使用以下方式调用脚本时,它们将被忽略:bash script_name,而不是使脚本文件可执行,并调用./ script_name
    选项应用set -x在代码中明确设置,或者最好使用其更简单的长名称(示例:set -o xtrace而不是set set -x < /代码>)。
  2. 第二个不良练习是使用-e选项或set -o errexit,它仅用于旧版POSIX遵守,但在不同情况下的行为如此不一致;它导致的错误比避免的错误更多。

以下是有关这些set -eset -o errexit Quirks and Issess的一些引用:

This is adding shell options to the shebang:

  • -x, -o xtrace: Trace display executed statements.
  • -e, -o errexit: Exit on error, but inconsistently for legacy POSIX compliance.
#!/bin/bash -xe

This shebang setting combines two discouraged bad practices with their own set of issues:

  1. First bad practice is setting shell options in the shebang; 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.
    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).
  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; that it causes more bugs than it avoids.

Here are some references about those set -e, set -o errexit quirks and issues:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文