【问题标题】:why does this single quoted string get interpreted when it's inside of a command substitution为什么这个单引号字符串在命令替换中时会被解释
【发布时间】:2016-06-13 23:53:26
【问题描述】:

比较这两行shell脚本:

printf '%s' 's/./\\&/g'                    #1, s/./\\&/g
printf '%s' `printf '%s' 's/./\\&/g'`      #2, s/./\&/g

我的问题是:为什么单引号双反斜杠被解释为第二行脚本的单个反斜杠?

【问题讨论】:

    标签: shell sh


    【解决方案1】:

    printf '%s' `printf '%s' 's/./\\&/g'`
    

    反引号内的表达式返回s/./\\&/g,与第一个表达式一样,没有单引号,所以你得到

    printf '%s' s/./\\&/g
    

    第一个反斜杠转义第二个反斜杠,因此它打印s/./\&/g

    【讨论】:

    • 我可以再问你一个相关的问题,为什么打印的是 &&&,而不是 abc ? echo "`echo 'abc' | sed 's/./\\&/g'`"
    • @pdg 你应该问另一个问题。
    • 好的,我再等一个小时再问。还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2016-07-30
    • 2019-09-14
    • 2011-07-08
    • 2022-01-04
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多