如何正确嵌套 Bash 反引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2657012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly nest Bash backticks
提问by hhh
Either I missed some backlash or backlashing does not seem to work with too much programmer-quote-looping.
要么我错过了一些强烈反对,要么反对似乎不适用于过多的程序员引用循环。
$ echo "hello1-`echo hello2-\`echo hello3-\`echo hello4\`\``"
hello1-hello2-hello3-echo hello4
Wanted
通缉
hello1-hello2-hello3-hello4-hello5-hello6-...
回答by Joey Adams
Use $(commands)
instead:
使用$(commands)
来代替:
$ echo "hello1-$(echo hello2-$(echo hello3-$(echo hello4)))"
hello1-hello2-hello3-hello4
$(commands)
does the same thing as backticks, but you can nest them.
$(commands)
与反引号做同样的事情,但你可以嵌套它们。
You may also be interested in Bash range expansions:
您可能还对 Bash 范围扩展感兴趣:
echo hello{1..10}
hello1 hello2 hello3 hello4 hello5 hello6 hello7 hello8 hello9 hello10
回答by YOU
if you insist to use backticks, following could be done
如果您坚持使用反引号,则可以执行以下操作
$ echo "hello1-`echo hello2-\`echo hello3-\\`echo hello4\\`\``"
you have to put backslashes, \\ \\\\ \\\\\\\\
by 2x and so on, its just very ugly, use $(commands)
as other suggested.
你必须把反斜杠,\\ \\\\ \\\\\\\\
2x 等等,它只是非常丑陋,$(commands)
按照其他建议使用。
回答by toobsco42
Any time you want to evaluate a command use command substitution
:
任何时候你想评估一个命令使用command substitution
:
$(command)
Any time you want to evaluate an arithmetic expression use expression substitution
:
任何时候你想计算一个算术表达式,请使用expression substitution
:
$((expr))
You can nest these like this:
您可以像这样嵌套这些:
Let's say file1.txt is 30 lines long and file2.txt is 10 lines long, than you can evaluate an expression like this:
假设 file1.txt 是 30 行长,file2.txt 是 10 行长,那么您可以评估这样的表达式:
$(( $(wc -l file1.txt) - $(wc -l file2.txt) ))
which would output 20 ( the difference in number of lines between two files).
这将输出 20(两个文件之间的行数差异)。
回答by Mark Rushakoff
It's a lot easier if you use bash's $(cmd)
command substitution syntax, which is much more friendly to being nested:
如果您使用 bash 的$(cmd)
命令替换语法,那就容易多了,这对嵌套更友好:
$ echo "hello1-$(echo hello2-$(echo hello3-$(echo hello4)))"
hello1-hello2-hello3-hello4
回答by G. C.
Sometimes backtick nesting can be substituted with xargs
and pipes
有时反引号嵌套可以用xargs
和管道代替
$ echo hello4 | xargs echo hello3 | xargs echo hello2 | xargs echo hello1
hello1 hello2 hello3 hello4
Drawback of this solution are:
这种解决方案的缺点是:
- All arguments must be provided in reverse order (4→1);
All arguments become space separated (solvable with
tr
):$ echo hello4 | xargs echo hello3 | xargs echo hello2 | xargs echo hello1 | tr ' ' '-' hello1-hello2-hello3-hello4
- 所有参数必须以相反的顺序提供(4→1);
所有参数变为空格分隔(可使用 解决
tr
):$ echo hello4 | xargs echo hello3 | xargs echo hello2 | xargs echo hello1 | tr ' ' '-' hello1-hello2-hello3-hello4
让我们展示一个真实的用例。
Following commands work in bash, but not in tcsh (backtick nesting is not handled very good in tcsh)
以下命令在 bash 中有效,但在 tcsh 中无效(反引号嵌套在 tcsh 中处理得不是很好)
$ ls $(dirname $(which bash))
$ ls `dirname \`which bash\``
They can be substituted with
它们可以替换为
$ which bash | xargs dirname | xargs ls