How to avoid echo "to be stored in a file?
The quote does not need to be escaped, but characters appearing after the 1st quote will be considered quoted even if there is no closing quote, so trailing redirection will not work if the quote is not escaped.
Just redirect the echo of a single quote to a file without escaping - just move the redirection forward.
>file.txt echo " The complete answer is a bit complicated, because the quote system is a state machine. If it is currently turned off, the next following quote turns it to βonβ if the quote is not selected as ^" . After the quotation machine isβ on β, the next quote will always turn it off - turning off the quote cannot be escaped.
Here is a small demonstration
@echo off :: everything after 1st quote is quoted echo 1) "this & echo that & echo the other thing echo( :: the 2nd & is not quoted echo 2) "this & echo that" & echo the other thing echo( :: the first quote is escaped, so the 1st & is not quoted. :: the 2nd & is quoted echo 3) ^"this & echo that" & echo the other thing echo( :: the caret is quoted so it does not escape the 2nd quote echo 4) "this & echo that^" & echo the other thing echo( :: nothing is quoted echo 5) ^"this & echo that^" & echo the other thing echo( And here are the results
1) "this & echo that & echo the other thing 2) "this & echo that" the other thing 3) "this that" & echo the other thing 4) "this & echo that^" the other thing 5) "this that" the other thing Adding
While it is impossible to avoid a closing quote, you can use the delayed extension to either hide the closing quote or counteract it with phantom reopening the quote.
@echo off setlocal enableDelayedExpansion :: Define a quote variable named Q. The closing quote is hidden from the :: quoting state machine, so everything is quoted. set Q=" echo 6) "this & echo that!Q! & echo the other thing echo( :: The !"! variable does not exist, so it is stripped after all quoting :: has been determined. It functions as a phantom quote to counteract :: the closing quote, so everything is quoted. echo 7) "this & echo that"!"! & echo the other thing results
6) "this & echo that" & echo the other thing 7) "this & echo that" & echo the other thing