Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why CMD escape character does magic instead of escaping new line

I try to execute simple script:

set list="a",^
"b",^
c

echo %list%

And the output is

"a","b",a","b",^

Although I expected it to be "a", "b", c. It seems that the quotation marks spoil everything because if b is without them, all works fine. It is also mysterious to me why it breaks only on second new line escape.

So, why the output is so strange? I tried to launch the script on Windows 7 if it matters

like image 286
EGeorge Avatar asked Jan 29 '26 10:01

EGeorge


2 Answers

Interesting question! It seems that when the ^ is placed at end of line, the first character in next line is not parsed, but literally inserted in the result without being processed by the parser. If such character must be balanced (like quotes or parentheses) then this cause strange "errors" that otherwise would be associated to "unbalanced characters".

If you change the first char. in the line after ^, the problem disappear. For example, inserting a space:

set list="a",^
 "b",^
 c

... or moving the commas:

set list="a"^
,"b"^
,c

... or just deleting the quote (as you already tested).

like image 107
Aacini Avatar answered Jan 31 '26 06:01

Aacini


As Aacini said, a multiline caret removes the first line feed and escapes the next character.
This is often used to create a variable containing a line feed.

set LF=^


REM Two empty lines here creates a line feed in LF.

To avoid the escaping of the character, you could use a redirect (obviously).

set list="a",<nul ^
"b",^
c

echo %list%

More about carets and the multiline behaviour at SO:Long commands split over multiple lines..

A bit about the redirection effects on multi line carets at Dostips:Comments without increasing macro size

like image 34
jeb Avatar answered Jan 31 '26 06:01

jeb