difficulty escaping quote(s)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

difficulty escaping quote(s)

#1 Post by taripo » 01 Jan 2015 11:21

I do not understand why the quote here isn't getting escaped

I'll show what to me is expected, and what to me isn't..

Code: Select all

C:\>type a.bat <ENTER>
echo p1=%1 p2=%2 p3=%3
C:\>

OK

Code: Select all

C:\>a rrrrr zzzzzz  <ENTER>

C:\>echo p1=rrrrr p2=zzzzzz p3=
p1=rrrrr p2=zzzzzz p3=

OK

Code: Select all

C:\>a rrrrr" zzzzzz" <ENTER>

C:\>echo p1=rrrrr" zzzzzz" p2= p3=
p1=rrrrr" zzzzzz" p2= p3=

OK


Now here is unexpected. I want to make both quotes literal. Thus making the spaces special. So i'd expect the spaces to break it so p1=rrrrr" and p2=zzzzzz"
but I get the whole thing in p1

Code: Select all

C:\>a rrrrr^" zzzzzz^"

C:\>echo p1=rrrrr" zzzzzz" p2= p3=
p1=rrrrr" zzzzzz" p2= p3=



Why are the quotes not getting escaped?

Even when I try just one literal quote it doesn't work. The quote is treated as special by the shell

Code: Select all

C:\>a rrrrr^" zzzzzz

C:\>echo p1=rrrrr" zzzzzz p2= p3=
p1=rrrrr" zzzzzz p2= p3=

C:\>


I'd expect p1=rrrrr" p2=zzzzzz

but instead I get p1 equals the whole thing. I want the quote to be literal, so the space special, and so the space i'd expect, to break the parameters. Why isn't it?

Happy New Year

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: difficulty escaping quote(s)

#2 Post by penpen » 01 Jan 2015 13:29

I don't know it, but i assume the comman line interpreter (CLI) processes a given command line by variable replacement then escaping, and finally divide the command line in cmd_token (%0) and param_token (%*).
If you want to access parameter 1 (,2, ...) the CLI parses the param_token not using escape characters, but knowing strings.

penpen

pieh-ejdsch
Posts: 257
Joined: 04 Mar 2014 11:14
Location: germany

Re: difficulty escaping quote(s)

#3 Post by pieh-ejdsch » 05 Jan 2015 04:40

hello,

A For loop is also the parameters individually like this, as they appear in the Batch.
The variable must be escaped and are given in quotation marks.
The enclosing quotation marks must then be removed.

The last parameter must not necessarily be enclosed in quotes.

Code: Select all

set param1=abc"
set param2=xyz"

cmd /v/c for %i in ("^!Param1!" !Param2!) do @echo %~i


Phil

Post Reply