Bash子shell:括号:()VS美元括号:$() [英] Bash subshell: parentheses:() VS dollar-parentheses:$()

查看:75
本文介绍了Bash子shell:括号:()VS美元括号:$()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bash中,() $()都创建一个子shell.

In bash, both () and $() create a subshell.

彼此之间有什么区别?它们的典型用法是什么?

What's the difference between each other? What's their typical usages?

推荐答案

()只是创建一个复合命令,并在括号内运行这些命令. $()可以做到这一点,但也可以替代输出.

() just creates a compound command, running the commands inside the parentheses. $() does the same, but also substitutes the output.

从文档中

  1. (列表)
    list 在子shell环境中执行...变量分配和内置命令执行完毕后,影响外壳环境的命令不会继续生效.退货状态为 list 的退出状态.

  1. (list)
    list is executed in a subshell environment ... 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.

命令替换
命令替换允许命令的输出替换命令名称.有两种形式:

Command Substitution
Command substitution allows the output of a command to replace the command name. There are two forms:

$(command)

`command`

Bash通过执行 command 并将命令替换替换为命令的标准输出来执行扩展,删除所有尾随的换行符.

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.

这篇关于Bash子shell:括号:()VS美元括号:$()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆