3

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

EGeorge
  • 148
  • 8
  • 2
    Odd - on my machine, also using Win7, it sets `list` to `"a","b",^` and tries to execute `c`. Since `list` terminates `^`, the `echo` produces `"a","b",` followed by the contents of the line following the `echo`... – Magoo Feb 10 '16 at 08:56
  • @Magoo Do you know why it sets list to "a","b",^? Why it does not count the second ^ as an escape character as it does with the first one? On my machine it tries to execute c if set is not followed by echo (or at least does it verbose), and I don't know shy its behavior is different in this case too. – EGeorge Feb 10 '16 at 09:13

2 Answers2

2

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

Aacini
  • 65,180
  • 12
  • 72
  • 108
1

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

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225