So I learned today that a newline can be set via the following command:
set nl=^&echo.
For example:
set nl=^&echo.
echo Hello%nl%world
yields
Hello
world
But why does this work? What is the significance of ^&?
So I learned today that a newline can be set via the following command:
set nl=^&echo.
For example:
set nl=^&echo.
echo Hello%nl%world
yields
Hello
world
But why does this work? What is the significance of ^&?
Place the code inside a .bat file, and do not set echo to off (leave echo on) and you will see how the commands are being expanded and executed.
set nl=^&echo.
echo One%nl%Two%nl%Three
C:\>set nl=&echo.
C:\>echo One & echo.Two & echo.Three
One
Two
Three
The ^
escapes the &
special character so that it is a literal character able to be set inside the nl
variable. Then when the nl
variable is expanded, the &echo.
is inserted.
All is left is to deconstruct the &echo.
part. The ampersand &
means that a new command starts on the same line. That new command on the same line is echo.
, which outputs a new line.