pdg pdg - 9 months ago 46
Bash Question

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

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?


Starting from

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

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

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

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