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))
一样容易地嵌套在其内部.还存在其他差异,例如反引号/标记版本中反斜杠的解析方式等.
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屋!