how do you explain this with escaping quotes in cmd

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

how do you explain this with escaping quotes in cmd

#1 Post by taripo » 13 Aug 2022 15:19

I would have thought that of these quotes


Code: Select all

C:\>echo "zzz^"abc^""
"zzz^"abc""

C:\>

Code: Select all

   echo "zzz^"abc^""
quotes 1,2,3,4   
1- the quote before the zzz
2- between the zzz and the abc there is a ^" So the quote there.
3- after the abc there is ^"  , the quote there.
4- There is a quote at the end


The first one has special meaning

The second one is attempted to be escaped

The third one is attempted to be escaped

The fourth one has special meaning and is paired with the first one. (Or turns the special meaning created by the first one, off).

Turns out the second one didn't get escaped. Got treated as special, so turned off the quoting So the third one at least got escaped..

Is it even possible to escape a quote when the special quoting meaning is on..

So, after the first quote, special meaning of quotes is On. So then ^ doesn't escape anything and gets sent literally. And the second quote ends up closing the special meaning.

What I was intending was, Turn quoting on and write a quote (that's for the first quote, that worked). Then write zzz. Then I intended that the second quote is escaped and wouldn't turn off the special meaning of quotes. Then I intended for abc to be written. And then (with special meaning still on), escape a quote(the third quote). Then print a quote(fourth quote). that also has special meaning and turns quoting off.

I see it doesn't work like that.. which makes me wonder..

Is it possible to have a literal quote within quotes?

I thought this might

Code: Select all

C:\>echo """
"""
and maybe it does, but that needs 3 quotes one after the other.

but if I want to say a b c " d e f as one argument.. I need quotes around the 7 characters, and I need the middle quote to be literal.

Can that be done?
Last edited by taripo on 13 Aug 2022 16:59, edited 4 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do you explain this with escaping quotes in cmd

#2 Post by aGerman » 13 Aug 2022 16:28

Actually the behavior is pretty straightforward. Just simplify your examples a little:

Code: Select all

echo ^"
outputs

Code: Select all

"
Not surprising. right?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code: Select all

echo "^"
outputs

Code: Select all

"^"
Why? Because the first quote starts a quoted sequence wherein the caret is treated as a literal character.


In your example:
echo "zzz^"abc^""
(posted without code tags because of color formatting)
The quoted substring is marked in red. The unquoted substring is marked in blue. The last quote is marked in red again because it actually introduces the next quoted substring.

However, I guess ECHO output is not the reason for your question. Things can get much more complicated depending on the context.

Steffen

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

Re: how do you explain this with escaping quotes in cmd

#3 Post by taripo » 13 Aug 2022 17:17

ok thanks

so

How would you explain this

Code: Select all

C:\Users\User>echo ab^"c | find """
FIND: Parameter format not correct

C:\Users\User>echo ab^"c | find """"
ab"c

C:\Users\User>where grep
c:\cygwin\bin\grep.exe

C:\Users\User>echo ab^"c | grep """
ab"c

C:\Users\User>echo ab^"c | grep """"
ab"c

C:\Users\User>

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do you explain this with escaping quotes in cmd

#4 Post by aGerman » 13 Aug 2022 17:39

While ECHO is an internal functionality of cmd.exe you're facing now the parsing of command line arguments in external tools. And here the behavior does heavily depend on how the developers implemented the tokenization of the command line. The standard tokenizers in C and C++ on Windows would have required that literal quotes are escaped with a backslash. In case of both findstr and grep it seems that the developers wrote their own tokenizers though.
In short: Read the documentation of these tools and try to figure out how quotes are handled. If you can't find anything about it you have no better chance than try and error. Use whatever works with those utilities.

Steffen

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: how do you explain this with escaping quotes in cmd

#5 Post by aGerman » 14 Aug 2022 05:02

Addendum: Of course you have to keep in mind that calling these tools from within a Batch script means that cmd.exe parses the command line first. You still might be forced to escape quotes and other special characters in order to meet the syntax required by the command line interpreter.

Steffen

Post Reply