shell 编程中的 $(command) 和 `command` 有什么区别? [英] What is the difference between $(command) and `command` in shell programming?
问题描述
要将命令的输出作为变量存储在 sh/ksh/bash 中,您可以执行以下任一操作
To store the output of a command as a variable in sh/ksh/bash, you can do either
var=$(command)
或
var=`command`
这两种方法有什么区别?
What's the difference if any between the two methods?
推荐答案
反引号/墓碑已被弃用,取而代之的是 $()
用于命令替换,因为 $()
可以像 $(echo foo$(echo bar))
一样轻松地嵌套在自身中.还有其他差异,例如反斜杠在反引号/gravemark 版本中的解析方式等.
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.
请参阅 BashFAQ/082 的几个原因总是更喜欢$(...) 语法.
See BashFAQ/082 for several reasons to always prefer the $(...) syntax.
另见POSIX 有关各种差异的详细信息的规范.
Also see the POSIX spec for detailed information on the various differences.
这篇关于shell 编程中的 $(command) 和 `command` 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!