加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash中的括号 – 子shell与分组

发布时间:2020-12-15 17:02:36 所属栏目:安全 来源:网络整理
导读:在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 environ
在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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读