4 回答

TA貢獻1816條經(jīng)驗 獲得超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條經(jīng)驗 獲得超13個贊
gcc
libdir=$(dirname $(dirname $(which gcc)))/lib libdir=`dirname \`dirname \\\`which gcc\\\`\``/lib

TA貢獻1836條經(jīng)驗 獲得超5個贊
`...`
$(...)
反斜杠( \
)內(nèi)背以一種不明顯的方式處理: $ 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`
因為 $()
強制執(zhí)行一個全新的引用上下文,因此每個命令替換都受到保護,并且可以單獨處理,而不需要特別關注引用和轉義。當使用背面,它變得越來越丑后,兩個以上的水平。 很少有更多的例子: echo `echo `ls`` # INCORRECTecho `echo \`ls\`` # CORRECTecho $(echo $(ls)) # CORRECT
它解決了使用反引號時行為不一致的問題: echo '\$x'
產(chǎn)出 \$x
echo `echo '\$x'`
產(chǎn)出 $x
echo $(echo '\$x')
產(chǎn)出 \$x
Backticks語法對嵌入命令的內(nèi)容有歷史限制,不能處理一些包含反引號的有效腳本,而較新的 $()
表單可以處理任何類型的有效嵌入式腳本。 例如,這些其他有效的嵌入式腳本不在左列中工作,而是在右側工作。 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條經(jīng)驗 獲得超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.
添加回答
舉報