简体   繁体   English

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

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

I can pass a double quote, and a larger than sign to any command in several ways: '"' , "\\"" , ">" 我可以用几种方式传递一个双引号和一个大于号的符号: '"'"\\""">"

But when i try to pass them together 但是当我试图将它们传递到一起时

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

Same with "\\"\\>" . I could make it work with single quotes around, but since i already have so much going on with dealing with quotes i'd like to keep it all inside double quotes. "\\"\\>" 。我可以使用单引号,但由于我已经有很多处理引号,我想将它全部保留在双引号内。

Is there any way to escape that? 有什么方法可以逃脱吗?

I'm on windows7 but i believe this is some backward compatibility 'feature', so not sure this info is relevant. 我在windows7上,但我相信这是一个向后兼容的“功能”,所以不确定这些信息是否相关。

Edit 1: 编辑1:

I tought Endoro had the right answer... but it's not that simple. 我认为恩多罗有正确的答案......但事情并非那么简单。 CMD treats ^> differently depending if there's a escaped double quote in the string. CMD会根据字符串中是否存在转义双引号来区别对待^> Anyone have any idea why?! 任何人都知道为什么?! or a different escaping method? 还是一种不同的逃避方法?

C:\>sh echo "\"^>"
">

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

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

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

Edit 2: here are the tests cases for what Monacraft suggested, using ^ before the quotes that go around the string 编辑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)

OK, I can't give you a full explanation, but if you put an escape character before the double quotes it works: 好的,我不能给你一个完整的解释,但如果你在双引号之前放一个转义字符就可以了:

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

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

I think by putting a ^ before the string, your telling cmd not to treat a ^ inside the string as part of the actual string. 我想通过在字符串之前放置一个^ ,告诉cmd不要将字符串中的^视为实际字符串的一部分。 Which is why: 这就是为什么:

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

Does that. 那样做。 However, I can't give you a full explanation, but at least that solves your problem. 但是,我不能给你一个完整的解释,但至少可以解决你的问题。

Edit 2: 编辑2:

Ok, for edit 2 All I can say is that you don't need to escape anything inside the string! 好的,编辑2我只能说你不需要逃避字符串内的任何东西!

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

Also found this website which explained that to escape a \\ all you need is \\\\ . 还发现这个网站解释说要逃避\\所有你需要的是\\\\ Click here for more Information. 点击此处获取更多信息。 For " simply double up the quotes ( "" ). 对于"简单地加倍引号( "" )。

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

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