如何逃避回声“存储在一个文件?

我知道: echo "blah blah" >file.txt作品。 而且echo "" >file.txt也可以。

但是,如果我想在一个文件中只回显一个" (双引号)。

echo ">file.txt不起作用,是否有可能做一个单行命令?

Windows外壳的转义字符是^ ,所以:

 echo ^" > file.txt 

报价不需要转义回显,但在第一次报价后出现的字符将被视为报价,即使没有结束报价,所以除非报价被转义,否则尾随重定向将不起作用。

将单引号的回显重定向到文件而不转义是简单的 – 只需将重定向移到前面即可。

 >file.txt echo " 

完整的答案有点复杂,因为报价系统是一个状态机。 如果当前“关闭”,则下一个报价将其打开,除非报价被转义为^" 。一旦报价机器处于“打开”状态,则下一个报价将始终关闭 – 报价不能转义。

这是一个小示范

 @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( 

这里是结果

 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 

附录

虽然不可能逃避收盘报价,但可以使用延时扩张来隐藏收盘报价,或者用幻影重新开盘报价来抵消收盘报价。

 @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 

结果

 6) "this & echo that" & echo the other thing 7) "this & echo that" & echo the other thing