为什么这个单引号字符串在命令替换中被解释 [英] why does this single quoted string get interpreted when it's inside of a command substitution

查看:40
本文介绍了为什么这个单引号字符串在命令替换中被解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比较这两行shell脚本:

Compare these two lines of shell script:

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

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

My question is: why does the single-quoted double backslashes get interpreted as a single backslash for the second line of script?

推荐答案

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

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

The expression inside backticks returns s/./\\&/g as in the first expression, without single quotes, so you get

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

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

The first backslash escapes the second one, so it prints s/./\&/g.

这篇关于为什么这个单引号字符串在命令替换中被解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆