我可以以多种方式向任何命令传递双引号和大于符号:'"'
,"\""
,">"
但当我尝试将它们一起传递时
C:\>echo "\">"
The system cannot find the path specified.
和"\"\>"
一样。我可以在单引号中让它起作用,但由于我已经处理了很多引号,所以我想把它们都放在双引号里。
有没有任何方法可以转义它?
我在Windows7上,但我认为这是一些向后兼容的“功能”,所以不确定这些信息是否相关。
编辑1:
我认为Endoro给出了正确答案......但事实并非如此简单。CMD会根据字符串中是否有转义的双引号而以不同方式处理^>
。有没有人知道为什么?或者有一个不同的转义方法吗?
C:\>sh echo "\"^>"
">
C:\>sh echo "a^>"
a^>
C:\>echo "\"^>"
"\">"
C:\>echo "a^>"
"a^>"
编辑2:
这里是Monacraft建议的测试用例,使用在字符串周围加上^
引号
C:\>echo ^"a/>"
The system cannot find the path specified.
(we still need to escape that > symbol)
C:\>echo ^"a/^>"
"a/>"
(work fine without \" in the string)
C:\>echo ^"\"/^>"
"\"/^>"
(add a single \" and the ^> escaping stop to works)
C:\>echo ^""/^>"
""/^>"
(ok, using ^ before the string means i dont have to escape the " anymore)
C:\>echo ^"^\"/^>"
"\"/^>"
(but what if i have an actual \" in my input that i have to escape... would ^\ prevent this from happening? nope)