\ does not escape
". Here's a quick proof and explanation:
echo "" & echo 1. (
& is a special char in cmd,
left & right means run
left then run
right.) We can see that both
echo "" and
echo 1 are run successfully.
echo " & 1234. We can see that the output is
" & 1234. This is because the opening
" has not been closed, and thus everything after it is interpreted as a string, including the special char
echo "\" & 1234.
\ does escape the following
", the opening
" will not be closed and the chars
& 1234 will be interpreted as part of the string.
\ fails to escape the following
", that following
" will close the string and
& 1234 will not be interpreted as part of the string.
In the output, we do not see
& 1234 interpreted as part of the string. This proves that
\ has failed to escape
So what escapes
" within quotes for argument passing? While
^ will work outside of quotes (easily proven via
echo ^" & echo 1), it does not escape quotes within quotes.
Indeed, how can we get something as simple as
echo 1 to work?
^ char? ...Nope,
echo "^"" & echo 1 outputs
"^"", and not
What about the
" char itself? ...Nope,
echo """" & echo 1 outputs
"""", and not
The fact is, there's nothing that will escape
" within quotes for argument passing. You can brood over this for a couple of years and arrive at no solution. This is just some of the inherent limitations of cmd scripting.
However, the good news is that you'll most likely never come across a situation whereby you need to do so. Sure, there's no way to get
echo 1 to work, but that's not such a big deal because it's simply a contrived problem which you'll likely never encounter.
For example, consider
runas. It works fine without needing to escape
" within quotes because
runas knew that there's no way to do so and made internal adjustments to work around it.
runas invented its own parsing rules (
runas /flag "anything even including quotes") and does not interpret cmd arguments the usual way. Official documentation for these special syntax is pretty sparse (or non-existent). Aside from
help, it's mostly trial-and-error.