How can I echo a newline in a batch file?
How can you you insert a newline from your batch file output?
I want to do something like:
Which would output:
echo hello & echo.world
This means you could define
& echo. as a constant for a newline
echo hello echo. echo world
Read more... Read less...
Here you go, create a .bat file with the following in it :
@echo off REM Creating a Newline variable (the two blank lines are required!) set NLM=^ set NL=^^^%NLM%%NLM%^%NLM%%NLM% REM Example Usage: echo There should be a newline%NL%inserted here. echo. pause
You should see output like the following:
There should be a newline inserted here. Press any key to continue . . .
You only need the code between the REM statements, obviously.
There is a standard feature
echo: in cmd/bat-files to write blank line, which emulates a new line in your cmd-output:
@echo off @echo line1 @echo: @echo line2
Output of cited above cmd-file:
Like the answer of Ken, but with the use of the delayed expansion.
setlocal EnableDelayedExpansion (set \n=^ %=Do not remove this line=% ) echo Line1!\n!Line2 echo Works also with quotes "!\n!line2"
First a single linefeed character is created and assigned to the \n-variable.
This works as the caret at the line end tries to escape the next character, but if this is a Linefeed it is ignored and the next character is read and escaped (even if this is also a linefeed).
Then you need a third linefeed to end the current instruction, else the third line would be appended to the LF-variable.
Even batch files have line endings with CR/LF only the LF are important, as the CR's are removed in this phase of the parser.
The advantage of using the delayed expansion is, that there is no special character handling at all.
echo Line1%LF%Line2 would fail, as the parser stops parsing at single linefeeds.
This doesn't answer the question, as the question was about single
echo that can output multiple lines.
But despite the other answers who suggests the use of
echo. to create a new line, it should be noted that
echo. is the worst, as it's very slow and it can completly fail, as cmd.exe searches for a file named
ECHO and try to start it.
For printing just an empty line, you could use one of
echo, echo; echo( echo/ echo+ echo=
But the use of
echo: should be avoided, as they can be really slow, depending of the location where the script will be executed, like a network drive.
echo. Enough said.
If you need it in a single line, use the
&. For example,
echo Line 1 & echo. & echo line 3
would output as:
Line 1 line 3
Now, say you want something a bit fancier, ...
set n=^&echo. echo hello %n% world
Then just throw in a
%n% whenever you want a new line in an echo statement. This is more close to your
\n used in various languages.
set n= sets the variable
n equal to:
^ Nulls out the next symbol to follow:
& Means to do another command on the same line. We don't care about errorlevel(its an echo statement for crying out loud), so no
&& is needed.
echo. Continues the echo statement.
All of this works because you can actually create variables that are code, and use them inside of other commands. It is sort of like a ghetto function, since batch is not exactly the most advanced of shell scripting languages. This only works because batch's poor usage of variables, not designating between ints, chars, floats, strings, etc naturally.
If you are crafty, you could get this to work with other things. For example, using it to echo a tab
set t=^&echo. ::there are spaces up to the double colon
When echoing something to redirect to a file, multiple echo commands will not work. I think maybe the ">>" redirector is a good choice:
echo hello > temp echo world >> temp