I have a batch script variable like this:
Set myvar=hello\nworld
And when I echo myvar, i get the whole hello\nworld literally. How can I ask it to parse my \n and intrepret it as a new line character, so that I can echo that in two lines?
I have a batch script variable like this:
Set myvar=hello\nworld
And when I echo myvar, i get the whole hello\nworld literally. How can I ask it to parse my \n and intrepret it as a new line character, so that I can echo that in two lines?
If your question really means "How to embed a new line character in a variable?", then this is the answer:
@echo off
setlocal EnableDelayedExpansion
set myvar=hello^
world
echo !myvar!
A caret ^
character means take the next character literally, but in this case the "next character" is just a new line, so it is taken literally in the variable and the command continue until the next end of line (as usual), so the set
command ends after the world
line. The embedded <NL>
can only be correctly processed via delayed !expansion!.
The embedded <NL>
is correctly processed in other cases; for example:
for /F %%a in ("!myvar!") do echo %%a
Imagine, there are different programming languages and \n
doesn't always mean "new line" ;).
echo hello&echo.world
will do what you want if you want to have it in one line of code. Otherwise, it's
echo hello
echo.world
In batch echo.
means new line or if .
is followed by some text it means echo the text in a new line.