bash shell 编程中的 $(command) 和 `command` 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4708549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:05:23  来源:igfitidea点击:

What is the difference between $(command) and `command` in shell programming?

bashshellkshsh

提问by hhafez

To store the output of a command as a variable in sh/ksh/bash, you can do either

要将命令的输出存储为 sh/ksh/bash 中的变量,您可以执行以下任一操作

var=$(command)

or

或者

var=`command`

What's the difference if any between the two methods?

如果有的话,这两种方法有什么区别?

回答by SiegeX

The backticks/gravemarks have been deprecated in favor of $()for command substitution because $()can easily nest within itself as in $(echo foo$(echo bar)). There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc.

反引号/墓碑已被弃用,取而代之的$()是命令替换,因为它$()可以很容易地嵌套在$(echo foo$(echo bar)). 还有其他差异,例如反斜杠在反引号/gravemark 版本中的解析方式等。

See BashFAQ/082for several reasons to always prefer the $(...) syntax.

请参阅BashFAQ/082的几个原因,始终更喜欢 $(...) 语法。

Also see the POSIXspec for detailed information on the various differences.

另请参阅POSIX规范以获取有关各种差异的详细信息。

回答by John Kugelman

They behave the same. The difference is syntactical: it's easier to nest $()than ``:

他们的行为相同。区别在于语法:嵌套$()``

listing=$(ls -l $(cat filenames.txt))

vs.

对比

listing=`ls -l \`cat filenames.txt\``

回答by VonC

July 2014: The commit f25f5e6(by Elia Pinto (devzero2000), April 2014, Git 2.0) adds to the nesting issue:

2014 年 7 月:提交 f25f5e6(由Elia Pinto ( devzero2000),2014 年 4 月,Git 2.0)增加了嵌套问题:

The backquoted form is the traditional method for command substitution, and is supported by POSIX.
However, all but the simplest uses become complicated quickly.
In particular, embedded command substitutions and/or the use of double quotes require careful escaping with the backslash character
.

反引号形式是传统的命令替换方法,POSIX 支持。
然而,除了最简单的用途之外,所有的用途都很快变得复杂。
特别是,嵌入的命令替换和/或双引号的使用需要小心地使用反斜杠字符转义

That is why the git/Documentation/CodingGuidelines mentions:

这就是为什么git/Documentation/CodingGuidelines提到:

We prefer $( ... )for command substitution; unlike ``, it properly nests.
It should have been the way Bourne spelled it from day one, but unfortunately isn't.

我们更喜欢$( ... )命令替换;不像``,它正确地嵌套
它应该是 Bourne 从第一天开始拼写的方式,但不幸的是不是。

thitoncommented:

thiton评论道

That is why `echo `foo``won't work in general because of the inherent ambiguity because each ``can be opening or closing.
It might work for special cases due to luck or special features.

这就是为什么`echo `foo``由于固有的歧义而一般不会起作用的原因,因为每个``都可以打开或关闭。
由于运气或特殊功能,它可能适用于特殊情况。



Update January 2016: Git 2.8 (March 2016) gets rid of backticks entirely.

2016 年 1 月更新:Git 2.8(2016 年 3 月)完全摆脱了反引号。

See commit ec1b763, commit 9c10377, commit c7b793a, commit 80a6b3f, commit 9375dcf, commit e74ef60, commit 27fe43e, commit 2525c51, commit becd67f, commit a5c98ac, commit 8c311f9, commit 57da049, commit 1d9e86f, commit 78ba28d, commit efa639f, commit 1be2fa0, commit 38e9476, commit 8823d2f, commit 32858a0, commit cd914d8(12 Jan 2016) by Elia Pinto (devzero2000).
(Merged by Junio C Hamano -- gitster--in commit e572fef, 22 Jan 2016)

提交ec1b763提交9c10377提交c7b793a提交80a6b3f提交9375dcf提交e74ef60提交27fe43e提交2525c51提交becd67f提交a5c98ac提交8c311f9提交57da049提交1d9e86f提交78ba28d提交efa639f提交1be2fa0提交38e9476提交8823d2f提交32858a0提交cd914d8(2016 年 1 月 12 日)作者:Elia Pinto ( devzero2000)
(由Junio C gitsterHamano合并-- --提交 e572fef 中,2016 年 1 月 22 日)

From Git 2.8 onwards, it is all $(...), no more `...`.

从 Git 2.8 开始,一切都是$(...),不再是`...`

回答by ocodo

When the older back-tick form is used, backslash retains its literal meaning except when followed by $, `, or \. The first back-tick not preceded by a backslash terminates the command substitution.

当使用较旧的反引号形式时,反斜杠保留其字面含义,除非后面跟有 $、` 或 \。前面没有反斜杠的第一个反引号终止命令替换。

When using the newer $(command)form, all characters between the parentheses make up the command; none are treated specially.

使用较新的$(command)形式时,括号之间的所有字符组成命令;没有被特殊对待。

Both forms can be nested, but the back-tick variety requires the following form.

两种形式都可以嵌套,但反引号种类需要以下形式。

`echo \`foo\`` 

As opposed to:

与之相反:

$(echo $(foo))

回答by DigitalRoss

There is little difference, except for what unescaped characters you can use inside of the command. You can even put `...`commands inside $(...)ones (and vice versa) for a more complicated two-level-deep command substitution.

除了可以在命令中使用的未转义字符之外,几乎没有区别。您甚至可以将`...`命令放在$(...) 中(反之亦然),以进行更复杂的两级深度命令替换。

There is a slightly different interpretation of the backslash character/operator. Among other things, when nesting `...`substitution commands, you must escape the inner `characters with \,whereas with $()substition it understands the nesting automatically.

对反斜杠字符/运算符的解释略有不同。除其他外,当嵌套`...`替换命令时,您必须用\转义内部的`字符而使用$()替换它会自动理解嵌套。

回答by Hiro

"What's the difference if any between the two methods?"

“如果有的话,这两种方法有什么区别?”

Make attention to this behaviour:

请注意这种行为:

A="A_VARIABLE"
echo "$(echo "$A")"
echo "`echo "$A"`"

You will get these results:

你会得到这些结果:

$A
A_VARIABLE