bash: 使用 set -e (errexit) 运行脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17076342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:40:38  来源:igfitidea点击:

bash: run a script with set -e (errexit)

bash

提问by asmeurer

Is there a way to run a bash script with set -e, i.e., as if set -ewere in the first line of the script? I know there is for example bash -xto emulate set -x, but I didn't see anything to emulate set -e.

有没有办法用set -e,即,好像set -e在脚本的第一行一样运行 bash脚本?我知道有例如bash -xto emulate set -x,但我没有看到任何可以模拟的东西set -e

回答by chepner

bashcan use any of the options that are valid with setas a command line option, so you can simply use

bash可以使用任何有效的选项set作为命令行选项,因此您可以简单地使用

bash -e myScript

Here's the first sentence from the Options section of the man page (emphasis mine; I know I glossed over this sentence for a long time as well):

这是手册页选项部分的第一句话(强调我的;我知道我也掩盖了这句话很长一段时间):

In addition to the single-character shell options documented in the description of the set builtin command, bash interprets the following options when it is invoked:

除了set 内置命令的描述中记录的单字符 shell 选项之外,bash 在调用时还会解释以下选项:

回答by ctn

How about this:

这个怎么样:

(set -e; . file.sh)