Bash中$()和()之间的区别 [英] Difference between $() and () in Bash
问题描述
当我键入ls -l $(echo file)
时,将括号中的输出(这只是简单的回显)输出并传递给外部ls -l
命令.等同于简单的ls -l file
.
When I type ls -l $(echo file)
output from bracket (which is just simple echo'ing) is taken and passed to external ls -l
command. It equals to simple ls -l file
.
当我键入ls -l (echo file)
时,出现错误,因为不能将()
嵌套在外部命令中.
When I type ls -l (echo file)
we have error because one cannot nest ()
inside external command.
有人可以帮助我了解$()
和()
之间的区别吗?
Can someone help me understand the difference between $()
and ()
?
推荐答案
$(cmd)
将cmd
的结果替换为字符串,而(cmd; cmd)
在子进程中运行命令列表.
$(cmd)
substitutes the result of cmd
as a string, whereas (cmd; cmd)
run a list of commands in a subprocess.
如果要将一个或多个命令的输出放入变量中,请使用$(cmd)形式.
If you want to put the output of one or more commands into a variable use the $( cmd ) form.
但是,如果您要运行多个命令并将它们视为一个单元 使用()表单.
However if you want to run a number of commands and treat them as a single unit use the () form.
当您想在后台运行一组命令时,后者非常有用:
The latter is useful when you want to run a set of commands in the background:
(git pull; make clean; make all) &
这篇关于Bash中$()和()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!