" 但是当我尝试将它们一起传递时 C:\>echo "\">" The system cannot find the path-6ren">
gpt4 book ai didi

windows - CMD/windows 批处理 : how to pass a double quote and a larger than sign in a command argument?

转载 作者:可可西里 更新时间:2023-11-01 14:14:53 26 4
gpt4 key购买 nike

我可以通过多种方式将双引号和大于号传递给任何命令:'"'"\"""> "

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

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)

最佳答案

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

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

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

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

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

那样做。但是,我无法给您一个完整的解释,但至少可以解决您的问题。

编辑 2:

好的,对于编辑 2 我只能说你不需要转义字符串中的任何内容!

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

还找到了这个网站,它解释说要转义 \,您只需要 \\Click here了解更多信息。对于 ",只需将引号加倍 ("")。

关于windows - CMD/windows 批处理 : how to pass a double quote and a larger than sign in a command argument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com