3

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 ^&?

Anonymous
  • 739
  • 7
  • 15
  • 1
    Oh, I have seen this, it is "code injection". – Endoro Jun 18 '13 at 20:14
  • 2
    See [Explain how dos-batch newline variable hack works](http://stackoverflow.com/q/6379619/1012053) for how to truly get a line feed character in a variable, with explanation and examples of how it can be used – dbenham Jun 19 '13 at 12:13

1 Answers1

9

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.

Batch

set nl=^&echo.
echo One%nl%Two%nl%Three

Output

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.

ixe013
  • 9,559
  • 3
  • 46
  • 77
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47