shell脚本中set -e是什么意思

我看了一下某个脚本,最开始的两行是

set -e
set -o pipefail

请问这两句是什么意思呢?

阅读 16.6k
2 个回答

$ set -e

这行代码之后的任何代码,如果返回一个非0的值,那么整个脚本立即退出,官方的说明是为了防止错误出现滚雪球的现象

$ set -o pipefail

原文解释如下:

If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status,or zero if all commands in the pipeline exit successfully. This option is disabled by default.

可理解为:

告诉 bash 返回从右到左第一个以非0状态退出的管道命令的返回值,如果所有命令都成功执行时才返回0

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题