在bash中有两种方法来捕获命令行的输出:

Legacy Bourne shell反引号' ': var =“命令” $()语法(据我所知是Bash特定的,或者至少不支持非posix旧shell,如原始Bourne) var = $(命令)

与反撇号相比,使用第二种语法有什么好处吗?还是两者完全相同?


当前回答

POSIX标准定义了命令替换的$(命令)形式。目前使用的大多数shell都是POSIX兼容的,并且支持这种首选形式,而不是古老的反刻度符号。Shell Language文档的命令替换部分(2.6.3)描述了以下内容:

Command substitution allows the output of a command to be substituted in place of the command name itself.  Command substitution shall occur when the command is enclosed as follows: $(command) or (backquoted version): `command` The shell shall expand the command substitution by executing command in a subshell environment (see Shell Execution Environment) and replacing the command substitution (the text of command plus the enclosing "$()" or backquotes) with the standard output of the command, removing sequences of one or more <newline> characters at the end of the substitution. Embedded <newline> characters before the end of the output shall not be removed; however, they may be treated as field delimiters and eliminated during field splitting, depending on the value of IFS and quoting that is in effect. If the output contains any null bytes, the behavior is unspecified. Within the backquoted style of command substitution, <backslash> shall retain its literal meaning, except when followed by: '$' , '`', or <backslash>. The search for the matching backquote shall be satisfied by the first unquoted non-escaped backquote; during this search, if a non-escaped backquote is encountered within a shell comment, a here-document, an embedded command substitution of the $(command) form, or a quoted string, undefined results occur. A single-quoted or double-quoted string that begins, but does not end, within the "`...`" sequence produces undefined results. With the $(command) form, all characters following the open parenthesis to the matching closing parenthesis constitute the command. Any valid shell script can be used for command, except a script consisting solely of redirections which produces unspecified results. The results of command substitution shall not be processed for further tilde expansion, parameter expansion, command substitution, or arithmetic expansion. If a command substitution occurs inside double-quotes, field splitting and pathname expansion shall not be performed on the results of the substitution. Command substitution can be nested. To specify nesting within the backquoted version, the application shall precede the inner backquotes with <backslash> characters; for example: \`command\` The syntax of the shell command language has an ambiguity for expansions beginning with "$((", which can introduce an arithmetic expansion or a command substitution that starts with a subshell. Arithmetic expansion has precedence; that is, the shell shall first determine whether it can parse the expansion as an arithmetic expansion and shall only parse the expansion as a command substitution if it determines that it cannot parse the expansion as an arithmetic expansion. The shell need not evaluate nested expansions when performing this determination. If it encounters the end of input without already having determined that it cannot parse the expansion as an arithmetic expansion, the shell shall treat the expansion as an incomplete arithmetic expansion and report a syntax error. A conforming application shall ensure that it separates the "$(" and '(' into two tokens (that is, separate them with white space) in a command substitution that starts with a subshell. For example, a command substitution containing a single subshell could be written as: $( (command) )

其他回答

最主要的一个是嵌套它们的能力,命令中的命令,而不会失去理智,试图弄清楚某种形式的转义是否对反节拍有效。

一个例子,虽然有点做作:

deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print)

它会给你一个/dir目录树中所有与2011年12月以来最早的文本文件同名的文件的列表(a)。

另一个例子是获取父目录的名称(而不是完整路径):

pax> cd /home/pax/xyzzy/plugh
pax> parent=$(basename $(dirname $PWD))
pax> echo $parent
xyzzy

(a)既然那个特定的命令可能实际上不起作用,我还没有测试它的功能。所以,如果你投我的反对票,你已经失去了意图:-)这只是一个说明你如何嵌套,而不是作为一个没有bug的生产准备片段。

来自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.

我想出了一个完全有效的$(…)除以'…'的例子。

我正在使用一个运行Cygwin的Windows远程桌面,并希望迭代一个命令的结果。遗憾的是,由于远程桌面或Cygwin本身的原因,反勾字符无法输入。

合理的假设是,在这种奇怪的设置中,美元符号和圆括号会更容易输入。

假设您想要找到与gcc安装位置对应的lib目录。你有一个选择:

libdir=$(dirname $(dirname $(which gcc)))/lib

libdir=`dirname \`dirname \\\`which gcc\\\`\``/lib

第一种方法比第二种简单——使用第一种方法。

$()允许嵌套。

out=$(echo today is $(date))

我觉得倒勾是不允许的。