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

发布于 2024-10-12 00:52:00 字数 152 浏览 7 评论 0 原文

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

var=$(command)

var=`command`

两种方法之间有什么区别?

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

var=$(command)

or

var=`command`

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

愁杀 2024-10-19 00:52:00

反引号/墓记已被弃用,转而使用 $() 进行命令替换,因为 $() 可以轻松地嵌套在其自身内,如 $(echo foo$(回声栏))。还有其他差异,例如反斜杠在反引号/坟墓标记版本中如何解析反斜杠等。

请参阅 BashFAQ/ 082 由于多种原因,我们总是更喜欢 $(...) 语法。

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

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.

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

Also see the POSIX spec for detailed information on the various differences.

昨迟人 2024-10-19 00:52:00

他们的行为是一样的。区别在于语法:嵌套 $() 比嵌套 `` 更容易

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

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

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\``
云裳 2024-10-19 00:52:00

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

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

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

我们更喜欢使用 $( ... ) 进行命令替换; 与``不同,它正确嵌套
Bourne 从第一天起就应该这么拼写,但不幸的是不是。

thiton 评论

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


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

请参阅提交 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 Hamano -- gitster -- 合并于 提交 e572fef,2016 年 1 月 22 日)

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

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

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
.

That is why the git/Documentation/CodingGuidelines mentions:

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.

thiton commented:

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.


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

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)

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

任性一次 2024-10-19 00:52:00

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

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

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

`echo \`foo\`` 

相对于:

$(echo $(foo))

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.

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

`echo \`foo\`` 

As opposed to:

$(echo $(foo))
秋风の叶未落 2024-10-19 00:52:00

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

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

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.

风追烟花雨 2024-10-19 00:52:00

“这两种方法有什么区别?”

请注意此行为:

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

您将得到以下结果:

$A
A_VARIABLE

 

"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

 

小瓶盖 2024-10-19 00:52:00

反引号和用于命令替换的较新的首选 POSIX 语法之间的一个新区别在于注释的处理方式。反引号内的散列字符创建以第二个反引号字符结束的注释,从而有效地创建了一种具有中行注释的方法。

$ echo "Goodbye` # Cruel` World"
Goodbye World

使用较新的首选语法,注释会持续到行尾,因此命令必须分为两行:

$ echo "Goodbye$( # Cruel
> ) World"
Goodbye World

如果您尝试在控制台中将其键入一行,您的终端将换行:

$ echo "Goodbye$( # Cruel) World"
> 

但是,您可以通过将命令传递给 sh -c 来将其强制为一行。然后你会看到它会导致语法错误:

$ sh -c 'echo "Goodbye$( # Cruel) World"'
sh: 1: Syntax error: end of file unexpected (expecting ")")

One novel difference between backticks and the newer preferred POSIX syntax for command substitution is in how comments are handled. A hash character within backticks creates a comment that ends at the second backtick character, effectively creating a way to have mid-line comments.

$ echo "Goodbye` # Cruel` World"
Goodbye World

Using the newer preferred syntax, the comment continues to the end of the line, so the command would have to be split across two lines:

$ echo "Goodbye$( # Cruel
> ) World"
Goodbye World

If you tried to type it into a console on a single line, your terminal would just wrap:

$ echo "Goodbye$( # Cruel) World"
> 

But, you can force it to one line by passing the command to sh -c. Then you'll see that it results in a syntax error:

$ sh -c 'echo "Goodbye$( # Cruel) World"'
sh: 1: Syntax error: end of file unexpected (expecting ")")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文