在bash中有两种方法来捕获命令行的输出:
Legacy Bourne shell反引号' ': var =“命令” $()语法(据我所知是Bash特定的,或者至少不支持非posix旧shell,如原始Bourne) var = $(命令)
与反撇号相比,使用第二种语法有什么好处吗?还是两者完全相同?
在bash中有两种方法来捕获命令行的输出:
Legacy Bourne shell反引号' ': var =“命令” $()语法(据我所知是Bash特定的,或者至少不支持非posix旧shell,如原始Bourne) var = $(命令)
与反撇号相比,使用第二种语法有什么好处吗?还是两者完全相同?
当前回答
来自man bash:
$(command) or `command` Bash performs the expansion by executing command and replacing the com- mand substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file). When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. The first backquote not preceded by a backslash terminates the command sub- stitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
其他回答
反引号('…')是只有最古老的不兼容POSIX的boure -shell才需要的遗留语法,而$(…)是POSIX,出于以下几个原因更受欢迎:
Backslashes (\) inside backticks are handled in a non-obvious manner: $ echo "`echo \\a`" "$(echo \\a)" a \a $ echo "`echo \\\\a`" "$(echo \\\\a)" \a \\a # Note that this is true for *single quotes* too! $ foo=`echo '\\'`; bar=$(echo '\\'); echo "foo is $foo, bar is $bar" foo is \, bar is \\ Nested quoting inside $() is far more convenient: echo "x is $(sed ... <<<"$y")" instead of: echo "x is `sed ... <<<\"$y\"`" or writing something like: IPs_inna_string=`awk "/\`cat /etc/myname\`/"'{print $1}' /etc/hosts` because $() uses an entirely new context for quoting which is not portable as Bourne and Korn shells would require these backslashes, while Bash and dash don't. Syntax for nesting command substitutions is easier: x=$(grep "$(dirname "$path")" file) than: x=`grep "\`dirname \"$path\"\`" file` because $() enforces an entirely new context for quoting, so each command substitution is protected and can be treated on its own without special concern over quoting and escaping. When using backticks, it gets uglier and uglier after two and above levels. Few more examples: echo `echo `ls`` # INCORRECT echo `echo \`ls\`` # CORRECT echo $(echo $(ls)) # CORRECT It solves a problem of inconsistent behavior when using backquotes: echo '\$x' outputs \$x echo `echo '\$x'` outputs $x echo $(echo '\$x') outputs \$x Backticks syntax has historical restrictions on the contents of the embedded command and cannot handle some valid scripts that include backquotes, while the newer $() form can process any kind of valid embedded script. For example, these otherwise valid embedded scripts do not work in the left column, but do work on the rightIEEE: echo ` echo $( cat <<\eof cat <<\eof a here-doc with ` a here-doc with ) eof eof ` ) echo ` echo $( echo abc # a comment with ` echo abc # a comment with ) ` ) echo ` echo $( echo '`' echo ')' ` )
Therefore the syntax for $-prefixed command substitution should be the preferred method, because it is visually clear with clean syntax (improves human and machine readability), it is nestable and intuitive, its inner parsing is separate, and it is also more consistent (with all other expansions that are parsed from within double-quotes) where backticks are the only exception and ` character is easily camouflaged when adjacent to " making it even more difficult to read, especially with small or unusual fonts.
来源:为什么$(…)比'…”(引号)?在BashFAQ
参见:
POSIX标准节“2.6.3命令替换” 包含$()语法的POSIX原理 命令替换 Bash-hackers:命令替换
除了其他答案,
$(...)
在视觉上更突出
`...`
反撇号看起来太像撇号了;这取决于你使用的字体。
(而且,正如我刚刚注意到的,在内联代码示例中输入反撇号要困难得多。)
来自man bash:
$(command) or `command` Bash performs the expansion by executing command and replacing the com- mand substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file). When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. The first backquote not preceded by a backslash terminates the command sub- stitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
假设您想要找到与gcc安装位置对应的lib目录。你有一个选择:
libdir=$(dirname $(dirname $(which gcc)))/lib
libdir=`dirname \`dirname \\\`which gcc\\\`\``/lib
第一种方法比第二种简单——使用第一种方法。
在2021年,有必要提到一个奇怪的事实,作为对其他答案的补充。
Microsoft DevOps YAML管道“脚本”可能包含Bash任务。但是,符号$()用于引用YAML上下文中定义的变量,因此在这种情况下,应该使用反引号来捕获命令的输出。
在将脚本代码复制到YAML脚本时,这主要是一个问题,因为DevOps预处理器对不存在的变量非常宽容,所以不会出现任何错误消息。