hhh*_*hhh 82 unix bash shell quoting
要么我错过了一些强烈反对或反对,似乎没有太多的程序员报价循环.
$ echo "hello1-`echo hello2-\`echo hello3-\`echo hello4\`\``"
hello1-hello2-hello3-echo hello4
Run Code Online (Sandbox Code Playgroud)
通缉
hello1-hello2-hello3-hello4-hello5-hello6-...
Run Code Online (Sandbox Code Playgroud)
Joe*_*ams 134
$(commands)改为使用:
$ echo "hello1-$(echo hello2-$(echo hello3-$(echo hello4)))"
hello1-hello2-hello3-hello4
Run Code Online (Sandbox Code Playgroud)
$(commands) 与反引号相同,但你可以嵌套它们.
您可能也对Bash范围扩展感兴趣:
echo hello{1..10}
hello1 hello2 hello3 hello4 hello5 hello6 hello7 hello8 hello9 hello10
Run Code Online (Sandbox Code Playgroud)
YOU*_*YOU 33
如果你坚持使用反引号,可以进行以下操作
$ echo "hello1-`echo hello2-\`echo hello3-\\\`echo hello4\\\`\``"
Run Code Online (Sandbox Code Playgroud)
你必须把反斜杠,\\ \\\\ \\\\\\\\2倍等等,它只是非常难看,使用$(commands)其他建议.
too*_*o42 10
任何时候您想要评估命令使用command substitution:
$(command)
Run Code Online (Sandbox Code Playgroud)
每当您想要评估算术表达式时,请使用expression substitution:
$((expr))
Run Code Online (Sandbox Code Playgroud)
您可以像这样嵌套:
假设file1.txt长30行,file2.txt长10行,你可以像这样评估一个表达式:
$(( $(wc -l file1.txt) - $(wc -l file2.txt) ))
Run Code Online (Sandbox Code Playgroud)
这将输出20(两个文件之间的行数差异).
如果你使用bash的$(cmd) 命令替换语法会更容易,这对嵌套来说要友好得多:
$ echo "hello1-$(echo hello2-$(echo hello3-$(echo hello4)))"
hello1-hello2-hello3-hello4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36691 次 |
| 最近记录: |