如何让一个 bash subshell 退出主调用 shell 脚本?

How can I make one bash subshell exit the main calling shell script?

具有以下 bash 脚本:

#!/bin/bash

set -e

function foo() {
  # commands that might fails and I want to exit my script
  ...
  echo "result I need as output"
}

my_var=$(foo)

echo "I don't want this if there is an error inside foo"

使用 set -e(在 bash 4.4.19 中)似乎不适用于子 shell,即最后的 echo 命令仍在执行)。如果 foo 中的任何命令以非零退出代码终止,我该如何编写代码使脚本退出。

我正在使用 bash GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin16.7.0) 并且调用我的脚本的结果是(点被替换为无效命令 head -this:

$ ./my_script
head: illegal option -- t
usage: head [-n lines | -c bytes] [file ...]
I don't want this if there is an error inside foo

根据评论更改

管道命令的退出状态是最后一个命令的退出状态,这可以使用 set -o pipefail 更改,因此管道退出状态将是 <>0 任何命令的退出状态是<>0.

第一个回答

当您使用 -e 选项时,函数 returns 一个非 0 退出代码就足够了,例如 return 1

在更一般的情况下(没有set -e),最好使用显式退出

my_var=$(foo) || exit 1

可能就足够了,因为错误可以被子 shell 写入标准错误(继承)。

否则仔细阅读手册可以解释为什么它没有像您预期的那样工作

set -e

Exit immediately if [...] returns a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of any command executed in a && or || list except the command following the final && or ||, [...].

This option applies to the shell environment and each subshell environment separately (see Command Execution Environment), and may cause subshells to exit before executing all the commands in the subshell.

[...]

并且来自命令执行环境

Subshells spawned to execute command substitutions inherit the value of the -e option from the parent shell. When not in POSIX mode, Bash clears the -e option in such subshells.