pdg pdg -5 years ago 139
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download