gcb*_*gcb 5 windows command-line arguments cmd batch-file
我可以通过一个双引号,并且比大牌子的任何命令在几个方面:'"',"\"",">"
但是当我尝试将它们一起传递时
C:\>echo "\">"
The system cannot find the path specified.
Run Code Online (Sandbox Code Playgroud)
与"\"\>". 我可以让它与单引号一起工作,但由于我已经有很多处理引号的工作,我想将它全部保留在双引号内。
有什么办法可以逃避吗?
我在 windows7 上,但我相信这是一些向后兼容的“功能”,所以不确定这个信息是否相关。
编辑1:
我认为 Endoro 的答案是正确的……但事情没那么简单。CMD^>根据字符串中是否有转义双引号进行不同处理。任何人都知道为什么?!或不同的转义方法?
C:\>sh echo "\"^>"
">
C:\>sh echo "a^>"
a^>
C:\>echo "\"^>"
"\">"
C:\>echo "a^>"
"a^>"
Run Code Online (Sandbox Code Playgroud)
编辑 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)
Run Code Online (Sandbox Code Playgroud)
好的,我无法给您完整的解释,但是如果您在双引号之前添加转义字符,它就会起作用:
C:\>echo "a^>"
"a^>"
C:\>echo ^"a^>"
"a>"
Run Code Online (Sandbox Code Playgroud)
我认为通过^在字符串之前放置 a ,您可以告诉 cmd 不要将^字符串内部的 a 视为实际字符串的一部分。这就是为什么:
C:\>echo "text^>" ^"text^>"
"text^>" "text>"
Run Code Online (Sandbox Code Playgroud)
这样做。但是,我无法给您完整的解释,但至少解决了您的问题。
好的,对于编辑 2 我只能说你不需要转义字符串内的任何内容!
C:\>echo ^"\"/>"
"\"/>"
Run Code Online (Sandbox Code Playgroud)
还发现这个网站解释说,要逃脱\你所需要的只是\\。点击这里查看更多信息。只需"将引号 ( "") 加倍即可。