The reason is what happens to a (double) quoted string.
As soon as the shell parser finds an (unquoted)
" it declares the whole string that ends on the next (unquoted)
" as quoted. Inside quoted strings, most backslashes don't get removed. In UN-quoted strings backslashes are removed.
From POSIX (remove most backslashes):
<backslash> that is not quoted shall preserve the literal value of the following character, …
This will show that in practice (set -x show what the shell executes after parsing):
$ ksh -c 'set -x; echo test\h\jtest; set +x'
+ echo testhjtest
unquoted backslashes get removed. But quoted ones don't :
$ ksh -c 'set -x; echo "test\h\jtest"; set +x'
+ echo 'test\h\jtest'
From POSIX Double quotes:
<backslash> shall retain its special meaning as an escape character (see Escape Character (Backslash)) only when followed by one of the following characters
That's why backslashes get removed inside quoted strings for:
$ ` " \ <newline>
$ ksh -c 'set -x; echo "test\"\h\j\"test"; set +x'
+ echo 'test"\h\j"test'
That is what happens when the first character found is a (double) quote.
When what is found is an initial backquote (
`) the parsing rules are different. First, the string starts unquoted (internal double quotes may start a quoted section) and there is only one quoting scope (until the closing (
`). Note the single
" on the first line below:
$ set -x; echo `echo \"\`echo \\"test\\" \`\"`; set +x
+++ echo '"test"'
++ echo '""test""'
+ echo '""test""'
+ set +x
$(…), instead, starts a new quoting scope every time.
$ set -x; echo $(echo \"$(echo \\"test\\" )\"); set +x
+++ echo '\test\'
++ echo '"\test\"'
+ echo '"\test\"'
+ set +x
The result is completely different.