4 回答
TA贡献1816条经验 获得超4个赞
deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print)
/dir
pax> cd /home/pax/xyzzy/plugh pax> parent=$(basename $(dirname $PWD))pax> echo $parent xyzzy
TA贡献1786条经验 获得超13个赞
gcc
libdir=$(dirname $(dirname $(which gcc)))/lib libdir=`dirname \`dirname \\\`which gcc\\\`\``/lib
TA贡献1836条经验 获得超5个赞
`...`$(...)
反斜杠( \)内背以一种不明显的方式处理: $ 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 \\
嵌套引用 $()要方便得多: echo "x is $(sed ... <<<"$y")"
而不是: echo "x is `sed ... <<<\"$y\"`"
或者写这样的东西: IPs_inna_string=`awk "/\`cat /etc/myname\`/"'{print $1}' /etc/hosts`因为 $()使用全新的上下文引用 这是不可移植的,因为伯恩和科恩外壳将需要这些反斜杠,而巴什和破折号不需要。 嵌套命令替换的语法更容易: x=$(grep "$(dirname "$path")" file)
超过: x=`grep "\`dirname \"$path\"\`" file`
因为 $()强制执行一个全新的引用上下文,因此每个命令替换都受到保护,并且可以单独处理,而不需要特别关注引用和转义。当使用背面,它变得越来越丑后,两个以上的水平。 很少有更多的例子: echo `echo `ls`` # INCORRECTecho `echo \`ls\`` # CORRECTecho $(echo $(ls)) # CORRECT
它解决了使用反引号时行为不一致的问题: echo '\$x'产出 \$xecho `echo '\$x'`产出 $xecho $(echo '\$x')产出 \$xBackticks语法对嵌入命令的内容有历史限制,不能处理一些包含反引号的有效脚本,而较新的 $()表单可以处理任何类型的有效嵌入式脚本。 例如,这些其他有效的嵌入式脚本不在左列中工作,而是在右侧工作。 IEEE :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 ')'`
$`"
$(...)`...`
TA贡献1860条经验 获得超8个赞
$(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.
添加回答
举报
