Bash - 如何在不创建子shell的情况下显式设置运算符优先级

Fre*_*Ben 7 shell bash subshell

我确定这是张贴在某个地方,但我一直找不到它。

在 Bash 中,如何在不创建子 shell 的情况下指定运算符优先级(又名命令分组)?在大多数其他语言中,()这样做,但在 Bash 中,它在“丢弃”环境更改的子外壳中运行命令。我想在不丢失环境更改的情况下指定运算符优先级。

具体来说,我想做这样的事情,并让整个脚本退出,而不仅仅是 中的子shell ()

die ()
{
    echo "[DIE]: $1"
    exit 1
}

# When installChruby returns an error, print the error message and exit
[[ $CHRUBY =~ [Yy] ]] && (installChruby || die "Error installing chruby")
Run Code Online (Sandbox Code Playgroud)

我通过这样做找到了一个“解决方法”,但它不是我想要的一个漂亮的单线:

if [[ $CHRUBY =~ [Yy] ]]; then installChruby || die "Error installing Chruby"; fi 
Run Code Online (Sandbox Code Playgroud)

期望的结果是什么都不做并在CHRUBY未设置时继续,installChruby如果CHRUBYY或则调用函数y,并且die仅当installChruby函数返回 false时才调用函数。

Bash 中是否有一个运算符可以执行此操作(),或者有没有办法告诉里面的代码()不在子 shell 中运行?

mic*_*has 6

来自man bash

   { list; }
          list  is  simply executed in the current shell environment.  list must be terminated with a newline or semicolon.
          This is known as a group command.  The return status is the exit status of list.  Note that unlike the  metachar?
          acters  (  and  ), { and } are reserved words and must occur where a reserved word is permitted to be recognized.
          Since they do not cause a word break, they must be separated from list by whitespace or another shell metacharac?
          ter.
Run Code Online (Sandbox Code Playgroud)