Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Edit 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. 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

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)
like image 430
gcb Avatar asked Oct 21 '22 02:10

gcb


1 Answers

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. 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:

Ok, for edit 2 All I can say is that you don't need to escape anything inside the string!

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 ("").

like image 128
Monacraft Avatar answered Oct 24 '22 03:10

Monacraft