要么我错过了一些强烈的反对或反对,似乎没有太多的程序员报价循环。
$ echo "hello1-`echo hello2-\`echo hello3-\`echo hello4\`\``"
hello1-hello2-hello3-echo hello4
通缉
hello1-hello2-hello3-hello4-hello5-hello6-...
答案 0 :(得分:123)
改为使用$(commands)
:
$ echo "hello1-$(echo hello2-$(echo hello3-$(echo hello4)))"
hello1-hello2-hello3-hello4
$(commands)
与反引号相同,但你可以嵌套它们。
您可能也对Bash范围扩展感兴趣:
echo hello{1..10}
hello1 hello2 hello3 hello4 hello5 hello6 hello7 hello8 hello9 hello10
答案 1 :(得分:31)
如果你坚持使用反引号,可以进行以下操作
$ echo "hello1-`echo hello2-\`echo hello3-\\\`echo hello4\\\`\``"
你必须把反斜杠,\\ \\\\ \\\\\\\\
放2倍,依此类推,它只是非常难看,使用$(commands)
和其他建议一样。
答案 2 :(得分:10)
只要您想评估命令,请使用command substitution
:
$(command)
每当您想要评估算术表达式时,请使用expression substitution
:
$((expr))
您可以像这样嵌套:
假设file1.txt长30行,file2.txt长10行,你可以评估这样的表达式:
$(( $(wc -l file1.txt) - $(wc -l file2.txt) ))
将输出20(两个文件之间的行数差异)。
答案 3 :(得分:9)
如果你使用bash的$(cmd)
command substitution syntax,这会更容易嵌套:
$ echo "hello1-$(echo hello2-$(echo hello3-$(echo hello4)))"
hello1-hello2-hello3-hello4
答案 4 :(得分:0)
有时反引号嵌套可以用xargs
和管道代替
$ echo hello4 | xargs echo hello3 | xargs echo hello2 | xargs echo hello1
hello1 hello2 hello3 hello4
此解决方案的缺点是:
所有参数都用空格分隔(可使用tr
解决):
$ echo hello4 | xargs echo hello3 | xargs echo hello2 | xargs echo hello1 | tr ' ' '-'
hello1-hello2-hello3-hello4
以下命令在bash中有效,但在tcsh中无效(反tick嵌套在tcsh中处理得不好)
$ ls $(dirname $(which bash))
$ ls `dirname \`which bash\``
它们可以替换为
$ which bash | xargs dirname | xargs ls