【发布时间】:2016-06-13 22:06:07
【问题描述】:
printf '%s' 'abc' | sed 's/./\\&/g' #1, \a\b\c
printf '%s' "`printf '%s' 'abc' | sed 's/./\\&/g'`" #2, &&&
第二个反引号内的表达式返回\a\b\c,我们有printf '%s' "\a\b\c",所以它应该打印\a\b\c。
我的问题是:为什么第二个脚本打印 &&& ?
注意:
我可以通过在每个反斜杠前面加上另一个反斜杠来获得第二个脚本工作(打印\a\b\c),但我不知道为什么需要它。
一个相关问题: why does this single quoted string get interpreted when it's inside of a command substitution
【问题讨论】:
-
printf '%s' "$(printf '%s' 'abc' | sed 's/./\\&/g')"工作正常 -
@anubhava 哇,真的。所以我猜背杆有一些奇怪的副作用。
-
是的,反引号很糟糕,真的没有理由使用它们。在此处查看有关您的问题的更多信息mywiki.wooledge.org/BashFAQ/082
-
是的,现在不鼓励使用反引号
-
@123 感谢您的链接,非常有用。
标签: shell sed sh backticks command-substitution