问题
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
回答1:
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).
回答2:
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
来源:https://stackoverflow.com/questions/35309545/why-cmd-escape-character-does-magic-instead-of-escaping-new-line