Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
5  
Came in useful for me. I had to do echo \n \n | my_app.exe in a script. I did (echo. && echo.) | my_app.exe – Vignesh Feb 18 '11 at 9:54
1  
Easy Approach " Code starts :" > echo hello&echo world , will give u what u need – prudviraj Jan 22 '15 at 13:59

14 Answers 14

up vote 255 down vote accepted

echo hello & echo.world

This means you could define & echo. as a constant for a newline.

share|improve this answer
9  
Also works without period: echo hello && echo world – Alexander Prokofyev Oct 10 '08 at 9:14
21  
the example doesn't need a period, but you do need one to echo a blank empty line: echo. && echo hello && echo. && echo world – matt wilkie Jun 16 '11 at 22:37
1  
Can you do this with a single echo so it can be redirected to a file? – Shahbaz Oct 25 '11 at 20:36
15  
@Shahbaz - $ (echo Hello && echo World) > ./File.txt – Nathan J. Brauer Nov 30 '11 at 5:28
15  
The period thing in "echo." never stops amazing me. It's so dated, and still I always forget that the dot must be strictly concatenated with the command name, with no spaces between. There's no error in the post of yours, I'm writing this just as a reminder: "echo ." != "echo." ! – quetzalcoatl Feb 10 '12 at 11:19

use

echo hello
echo.
echo world
share|improve this answer
5  
Is it possible while providing a string within a single echo statement? – Brian R. Bondy Sep 25 '08 at 11:52
2  
Why do you need to do it with a single echo statement; where's the harm in having another? I assume you're not simply adding unnecessary constraints for the fun of it... – Rob Sep 25 '08 at 11:54
2  
@Rob, I just came across this problem and none of these work. You need to echo in a single statement in my example. I am generating some tex files from HTML and generating a Makefile by using echo "Makefile contents (which has \n)" > Makefile With multiple echos, it wouldn't work – Shahbaz Oct 25 '11 at 20:35
    
@Rob, Of course we need a single statement..... How would you pipe the 3 echos to a file then? – Pacerier Jul 29 '15 at 4:36
    
@Rob, can't you just answer Brian R, Bondy's question? He doesn't ask for alternative approaches – Green Sep 24 '15 at 2:38

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.

share|improve this answer
29  
Very impressive, could you take time to explain how the line set NL=^^^%NLM%%NLM%^%NLM%%NLM% works? I can't quite get my head round it – Andy Morris Nov 12 '09 at 13:14
1  
Alas, this doesn't work when passing the echoed string to another program. – Joey Mar 18 '11 at 17:52
7  
@andy methinks a +8 comment warrants a question: stackoverflow.com/questions/6379619/… – matt wilkie Jun 16 '11 at 23:06
56  
This is a wonderful example to show that cmd.exe and Windows batch files are totally insane! – mivk Oct 15 '11 at 10:54
1  
Yeah, this is why for such things I usually install ruby :) – quetzalcoatl Feb 10 '12 at 11:28

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:

line1

line2
share|improve this answer
9  
echo, and echo. also work – BlueRaja - Danny Pflughoeft Jun 10 '11 at 15:50
22  
In fact, any UNUSED special char should work. I always encourage the use of / because the slash is the standard char for command options. Also, I always criticize the use of the dot that somebody in Microsoft unfortunately choose because the command: echo.com give different results depending on the version of MS-DOS/Windows Batch command processor. – Aacini Jul 30 '11 at 4:58
9  
The only version that always works is echo(. It looks like it could cause problems, but it actually works perfectly. All other forms have at least one situation where the command will not perform as desired. – dbenham Jun 19 '13 at 12:30
1  
@dbenham this is even more fascinating. do you know about examples or explanation about the situations you speak of? also, do you know about any designer story, memo, specification or manual that describes this feature of the dos and cmd command line? – n611x007 Apr 18 '14 at 16:48
2  
@Pacerier - there are examples of failure for each of those suggestions. Carefully read dostips.com/forum/viewtopic.php?p=4554#p4554. – dbenham Jul 29 '15 at 12:23

Like the answer of Ken, but with the use of the delayed expansion.

setlocal EnableDelayedExpansion
set LF=^


REM Two empty lines are necessary
echo Line1!LF!Line2

First a single linefeed character is created and assigned to the LF-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?

share|improve this answer
    
Ken's answer didn't work for me, but yours works like a charm (Windows 7). – Withheld Feb 12 '15 at 15:42

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
share|improve this answer
    
Great! exactly what I needed for redirecting to files. – Shahbaz Oct 25 '11 at 20:40
5  
The first example, using ">", will create a new file, the second one, using ">>", will append to an existing file (or create it if it doesn't already exist). – Yann Duran Aug 9 '12 at 15:27

Just like Grimtron suggests - here is a quick example to define it:

@echo off
set newline=^& echo.
echo hello %newline%world

Output

C:\>test.bat
hello
world
share|improve this answer
    
It will actually echo an additional newline after "world" – BlueRaja - Danny Pflughoeft Jun 10 '11 at 15:52
1  
@blue the trailing newline seems to be function of the batch file itself. If you repeat the echo hello %newline%world line there are no spaces between. – matt wilkie Jun 16 '11 at 22:59
    
+1 for the ^& operator – quetzalcoatl Feb 10 '12 at 11:31
2  
echo "Hello %newline% world" fails as it is inside of quotes. Because it isn't a real newline. – jeb Oct 20 '13 at 20:23

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
share|improve this answer
    
Plain and simple. Like it. – Jacques Aug 31 '15 at 15:25

If you need to put results to a file, you can use

(echo a & echo. & echo b) > file_containing_multiple_lines.txt
share|improve this answer

You can also do like this,

(for %i in (a b "c d") do @echo %~i)

The output will be,

a
b
c d

Note that when this is put in a batch file, '%' shall be doubled.

(for %%i in (a b "c d") do @echo %%~i)
share|improve this answer
    
This solution also works if you want to echo an ampersand & instead of a newline. – parvus Jun 26 '14 at 13:48

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 dreaded process_begin: CreateProcess(NULL, echo., ...) failed. error message.

I hope this helps at least one other person out there :)

share|improve this answer

This worked for me, no delayed expansion necessary:

@echo off
(
echo ^<html^> 
echo ^<body^>
echo Hello
echo ^</body^>
echo ^</html^>
)
pause

It writes output like this:

<html>
<body>
Hello
</body>
</html>
Press any key to continue . . .
share|improve this answer
3  
-1 echo asdf >myfile.txt will produce the exact same results. echo appends a newline to the end of the string. – BlueRaja - Danny Pflughoeft Jun 10 '11 at 15:51
    
Great answer, being the only person who recognized the value of using the parenthesis for new lines. I believe this is how the makers of DOS intended you to do it. – djangofan Dec 3 '13 at 15:48

You can use @echo ( @echo + [space] + [insecable space] )

Note: The insecable space can be obtained with Alt+0160

Hope it helps :)

[edit] Hmm you're right, I needed it in a Makefile, it works perfectly in there. I guess my answer is not adapted for batch files... My bad.

share|improve this answer
    
I got ECHO ist eingeschaltet (ON). not an empty line, tried at the cmd-prompt – jeb Jul 18 '13 at 13:29
    
That’s a non-breaking space, not a newline. – Ryan O'Hara Jul 18 '13 at 15:36

I can't make it any simpler than:

echo echo hello^&echo world^&pause>silly.bat                       
call silly.bat
share|improve this answer

protected by Sam Saffron Dec 20 '10 at 2:44

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.