How can you you insert a newline from your batch file output?
I want to do something like:
> echo hello\nworld
Which would output:
hello
world
How can you you insert a newline from your batch file output?
I want to do something like:
> echo hello\nworld
Which would output:
hello
world
If one needs to use famous \n in string literals that can be passed to a variable, may write a code like in the Hello.bat script below:
This way multiline output may by prepared in one place, even in other scritpt or external file, and printed in another.
The line break is held in newline variable. Its value must be substituted after the echo line is expanded so I use setlocal enableDelayedExpansion to enable exclamation signs which expand variables on execution. And the execution substitutes \n with newline contents (look for syntax at help set). We could of course use !newline! while setting the answer but \n is more convenient. It may be passed from outside (try Hello R2\nD2), where nobody knows the name of variable holding the line break (Yes, Hello C3!newline!P0 works the same way).
Above example may be refined to a subroutine or standalone batch, used like
call:mlecho Hi\nI'm your comuter
:Please note, that additional backslash won't prevent the script from parsing \n substring.
If anybody comes here because they are looking to echo a blank line from a MINGW make makefile, I used
@cmd /c echo.
simply using
echo.
causes the dreadedprocess_begin: CreateProcess(NULL, echo., ...) failed.
error message.I hope this helps at least one other person out there :)
When echoing something to redirect to a file, multiple echo commands will not work. I think maybe the ">>" redirector is a good choice:
Like the answer of Ken, but with the use of the delayed expansion.
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.More explanations are at
SO:Long commands split over multiple lines in Vista/DOS batch (.bat) file
SO:How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Edit: Avoid
echo.
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 thatecho.
is the worst, as it's very slow and it can completly fail, as cmd.exe searches for a file namedECHO
and try to start it.For printing just an empty line, you could use one of
But the use of
echo.
,echo\
orecho:
should be avoided, as they can be really slow, depending of the location where the script will be executed, like a network drive.Ken and Jeb solutions works well.
But the new lines are generated with only an LF character and I need CRLF characters (Windows version).
To this, at the end of the script, I have converted LF to CRLF.
Example:
Use: