question on the issue of escaping quotes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

question on the issue of escaping quotes

#1 Post by taripo » 10 Dec 2012 02:56

Inspired by an old thread, I looked a bit more into escaping quotes.


C:\>echo a | grep "\""

C:\>echo a | grep "\"" >g
grep: >g: No such file or directory

C:\>

It looks to me, and please correct me if i'm wrong, it looks like the formulation

of
" \" " <--- should be avoided - at least in I suppose any program that uses argsv, as it leads to a mismatch of quotes.

Though to escape a quote, this is fine """ And \"(without quotes around it) is fine.

Another thing that doesn't work with an argsv program, is ^" (though it's fine as an argument for echo which uses GetCommandLine)

C:\>echo a | grep "^"" >g
grep: >g: No such file or directory

so in the sense of tripping that error, it's like "\""

But ^" produces strange results with grep or any argsv program.

For example, if ^" behaved, then this shouldn't return anything.

Code: Select all

C:\>echo a| grep ^"
a

C:\>



I think it causes grep's argsv[1] to exist but with i'm not sure what in it. Multiple ^" are also strange.

But it seems to be the way to escape a quote when it's an argument to echo

Code: Select all

C:\>echo "f | grep "f"
"f | grep "f"

C:\>echo ^"f | grep "f"
"f

C:\>echo \"f | grep "f"
\"f | grep "f"

C:\>



So for example
this works

Code: Select all


C:\>echo ^"f|grep \">g
Using \" works with  an argsv program.

C:\>echo "f|grep f
"f|grep f
When one has an odd number of quotes as part of the echo, then one has to escape one or more if one wants to, so they're even in number.



C:\>echo \"f|grep f
\"f|grep f

(a \" with echo fails as echo is a GetCommandLine program so don't use it with echo!)

C:\>

C:\>echo ^"f|grep \"f
"f
Use ^" with echo as echo uses GetCommandLine




C:\>

Does anybody have any thoughts on what's happening?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: question on the issue of escaping quotes

#2 Post by dbenham » 10 Dec 2012 05:51

Actually the following does not work because the quote escapes the redirection:

Code: Select all

echo ^"f|grep \">g

Your problem is that you need to worry about escaping quote for the Windows batch parser and also for grep. For batch you need ^". For grep you need \". The batch escape is consumed before grep sees it. Combining both, you get a proper solution:

Code: Select all

echo ^"f|grep \^">g

Note that once quoting has begun in batch, it is impossible to escape the next quote. You can escape as many quotes as you want, but once batch sees an unescaped quote, then the ^ is treated as a literal until the next quote is encountered.


Dave Benham

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: question on the issue of escaping quotes

#3 Post by taripo » 11 Dec 2012 13:05

thanks, that's very thought provoking and enlightening - as I thought it'd be when I saw your username!

Post Reply