2
printf '%s' 'abc' | sed 's/./\\&/g'                        #1, \a\b\c
printf '%s' "`printf '%s' 'abc' | sed 's/./\\&/g'`"        #2, &&&

The expression inside the second backticks returns \a\b\c, and we have printf '%s' "\a\b\c", so it should print \a\b\c. My question is: why does the second script print &&& ?

note: I can get the second script work (prints \a\b\c) by prepending each backslash with another backslash, but I don't know why it's needed.

One related question: why does this single quoted string get interpreted when it's inside of a command substitution

4

2 回答 2

2

This is a good example to show difference between back-tick and $(cmd) command substitutions.

When the old-style backquoted form of substitution is used, backslash retains its literal meaning except when followed by "$", "`", or "\". The first backticks not preceded by a backslash terminates the command substitution. When using the "$(COMMAND)" form, all characters between the parentheses make up the command; none are treated specially.

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html

So take a look your example, I used echo instead of printf:

kent$  echo 'abc' | sed 's/./\\&/g'
\a\b\c

kent$  echo -E "`echo 'abc' | sed 's/./\\&/g'`"
&&&

kent$  echo -E "$(echo 'abc' | sed 's/./\\&/g')"                    
\a\b\c

You can see, the back-tick command substitution made your \\ as single \, thus together with the followed & it became \& (literal &)

Note that I used echo -E in order to disable the interpretation of backslash escapes so that the \a\b\c could be printed out.

于 2016-06-13T14:04:32.927 回答
1

Because on the second line :

you are saying:

printf '%s' 'abc' -> 'abc'

Then replace:

'abc'| sed 's/./\\&g' -> &&&

The s mean substitute
. mean one character
\\& by a char &
g mean multiple occurrence on the line

So you are saying:

Replace in abc each char by & multiple time on the same line

Explanation of \\\& :

Two backslashes become a single backslash in the shell which then in sed escapes the forward slash which is the middle delimiter.

\\& -> \& (which makes the forward & a regular character instead of a delimiter)

Three of them: The first two become one in the shell which then escape the third one in sed
\\\& -> \\&

Finally! don't forget that you command is under backquote:

The reason you have to escape it "twice" is because you're entering this command in an environment (such as a shell script) that interprets the double-quoted string once. It then gets interpreted again by the subshell.

From:

Why does sed require 3 backslashes for a regular backslash?

于 2016-06-13T13:49:30.650 回答