bash中的括号 – 子shell与分组

在bash的联机帮助页面中,在“ Compound Commands”部分下面有以下两个条目:

(list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell’s environment do not remain in effect after the command completes. The return status is the exit status of list.

根据test[[

( expression ) Returns the value of expression. This may be used to override the normal precedence of operators.

我能看到的唯一区别是,在一个中,括号旁边没有空格,而另一个则是.这实际上区分了分组与子shell,还是依赖于上下文?

换句话说,如果我跑

if ! [ 2 -eq 2 ] || ( [ 2 -eq 2 ] && [ 4 -eq 4 ] ); then echo "hello"; fi

这只是分组条件还是在子shell中运行?

这些条目的上下文是相关的.

后者在[[构造并记录该构造在其参数上的行为的文档]的文档中.

前者正在讨论顶级shell复合命令构造(如[[construct本身])并引入了一个子shell.

这在手册页的后面的test / [命令的描述中再次出现(但基本上与[[讨论]相同).

要在当前shell中进行分组,可以使用花括号:

if ! [ 2 -eq 2 ] || { [ 2 -eq 2 ] && [ 4 -eq 4 ]; }; then
    ...
fi

(注意括号内侧和额外分号周围的空格,这两者都是必要的.)

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...