繁体   English   中英

CMD / windows Batch:如何在命令参数中传递双引号和大于号?

[英]CMD/windows Batch: how to pass a double quote and a larger than sign in a command argument?

我可以用几种方式传递一个双引号和一个大于号的符号: '"'"\\""">"

但是当我试图将它们传递到一起时

C:\>echo "\">"
The system cannot find the path specified.

"\\"\\>" 。我可以使用单引号,但由于我已经有很多处理引号,我想将它全部保留在双引号内。

有什么方法可以逃脱吗?

我在windows7上,但我相信这是一个向后兼容的“功能”,所以不确定这些信息是否相关。

编辑1:

我认为恩多罗有正确的答案......但事情并非那么简单。 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)

好的,我不能给你一个完整的解释,但如果你在双引号之前放一个转义字符就可以了:

C:\>echo "a^>"
"a^>"

C:\>echo ^"a^>"
"a>"

我想通过在字符串之前放置一个^ ,告诉cmd不要将字符串中的^视为实际字符串的一部分。 这就是为什么:

C:\>echo "text^>" ^"text^>"
"text^>" "text>"

那样做。 但是,我不能给你一个完整的解释,但至少可以解决你的问题。

编辑2:

好的,编辑2我只能说你不需要逃避字符串内的任何东西!

C:\>echo ^"\"/>"
"\"/>"

还发现这个网站解释说要逃避\\所有你需要的是\\\\ 点击此处获取更多信息。 对于"简单地加倍引号( "" )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM