690

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
dreftymac
  • 27,818
  • 25
  • 108
  • 169
Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619
  • 7
    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 09:54
  • 2
    Easy Approach " Code starts :" > echo hello&echo world , will give u what u need – prudviraj Jan 22 '15 at 13:59
  • 2
    You can insert an invisible ascii chr(255) on a separate line which will force a blank new line. Hold down the [alt] key and press 255 on the keypad. this inserts chr(255) which is a blank square. i.e. "*echo (alt+255)*" You can only use the keypad not the numbers at the top of the querty keyboard! – jwzumwalt Nov 20 '18 at 04:54
  • Just as half of computer repair is plugging it in and turning it on, half of software development is what I call **space engineering**. We _need_ our blank lines just so. – Bob Stein Apr 07 '19 at 12:27
  • How About This ? `echo Yes&echo No` –  Aug 15 '19 at 13:24
  • 1
    @jwzumwalt thank you for the alt+255 suggestion, works great on the command line – K Vij Oct 22 '20 at 02:28
  • printf "\n\n\n" – Mark Dec 16 '20 at 02:12

19 Answers19

607

Use:

echo hello
echo.
echo world
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Matt Lacey
  • 64,328
  • 10
  • 87
  • 143
  • 17
    Is it possible while providing a string within a single echo statement? – Brian R. Bondy Sep 25 '08 at 11:52
  • 6
    Why do you need to do it with a single echo statement; where's the harm in having another? – Rob Sep 25 '08 at 11:54
  • 3
    @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 04:36
  • 2
    @Rob, can't you just answer Brian R, Bondy's question? He doesn't ask for alternative approaches – Green Sep 24 '15 at 02:38
  • 1
    Use `echo;` instead of `echo.`. For some reason, `echo.` will not work in the case that the working directory happens to have a file called `echo` in it already. However, `echo;` works fine. – Brandin Feb 07 '16 at 23:59
  • 4
    Use & (or &&) to do them in a single statement: For example, to put "hello\nworld" in a txt file: `(echo hello & echo world) >> ./test.txt` – Ned Martin Mar 23 '16 at 06:50
  • 4
    For multi-line output to a file why not just do the following? `echo line 1 > Makefile` and then `echo line 2 >> Makefile` . Use of `>>` causes the output to be appended to the file, which is precisely the behavior you need for this case. – dgnuff Jan 04 '18 at 16:49
  • `echo:` is recommended by [SS64](https://ss64.com/nt/echo.html). – Alejandro Blasco Mar 18 '20 at 08:12
497

echo hello & echo.world

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

Gerhard
  • 18,114
  • 5
  • 20
  • 38
Grimtron
  • 6,409
  • 3
  • 21
  • 28
  • 49
    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
  • 4
    Can you do this with a single echo so it can be redirected to a file? – Shahbaz Oct 25 '11 at 20:36
  • 52
    @Shahbaz - `$ (echo Hello && echo World) > ./File.txt` – Nathan J.B. Nov 30 '11 at 05:28
  • 26
    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
  • 2
    The final answer is missing: $ echo.>>myfile.txt this will add a line break in a file – Guillaume Bois Jun 21 '12 at 11:50
  • Just thought I'd add this. I wanted to view the system `PATH` variable from the command line, but easily readable. This post helped me, and I came up with this: `for /f "tokens=*" %i in ("%path%") do set a=%i && set (b=!a:;=^&echo.!)` then `echo %b%` and voila! Line by line output. – dgo Aug 16 '13 at 16:12
  • I have more info! `echo.` makes a new line. `echo` shows the text instead of `'hello' is not recognized as an internal or external command, operable program or batch file.` – MineCMD Oct 30 '14 at 02:46
  • @NathanJ.Brauer, Single `&` works too: `(echo Hello&echo World) > ./File.txt` – Pacerier Jul 29 '15 at 04:32
  • @mattwilkie, What's the difference between two `&&` instead of one `&`? – Pacerier Jul 29 '15 at 04:32
  • @Pacerier a single ampersand means "do this, then that" while double means "do this, and if no error then do that". Double pipe, `||`, is it's mate, " do this, if failure then do that". See http://ss64.com/nt/syntax-redirection.html – matt wilkie Jul 29 '15 at 04:47
  • 6
    @quetzalcoatl, it gets even stranger. Other characters besides dot work too. SS64 says better syntax is [`echo(`](http://ss64.com/nt/echo.html) for improved performance (still with no space mind you). Extended discussion of other characters and their merits/flaws at [ECHO. FAILS to give text or blank line - Instead use ECHO/](http://www.dostips.com/forum/viewtopic.php?t=774) – matt wilkie Jul 29 '15 at 05:09
  • @mattwilkie, Continue at [the comments](http://stackoverflow.com/questions/132799/how-can-you-echo-a-newline-in-batch-files#comment8188921_3123194) in the answer below.... – Pacerier Jul 29 '15 at 05:47
  • @mattwilkie - technically, "do this, then that" would be using a single semicolon (e.g. `echo hi; echo bye;`). Using a single ampersand can have the same result, but it would combine the two return values into one collective binary value rather than having two separate, independent return values (it's more complicated than this, but this is the beginning of the basics haha). – Nathan J.B. Jul 30 '15 at 05:22
  • 3
    @NathanJ.Brauer, this Command/CMD (Dos/Windows) not bash etc. Semicolon is just another character and doesn't have any effect. – matt wilkie Aug 24 '15 at 17:40
  • Note that the _space_ between `hello` and the `&` is echoed too! Just remove it to avoid that... – aschipfl Jun 08 '18 at 19:01
146

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.

jeb
  • 70,992
  • 15
  • 159
  • 202
  • 56
    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 `echo`ed string to another program. – Joey Mar 18 '11 at 17:52
  • 8
    @andy methinks a +8 comment warrants a question: http://stackoverflow.com/questions/6379619/explain-how-dos-batch-newline-variable-hack-works – matt wilkie Jun 16 '11 at 23:06
  • 90
    This is a wonderful example to show that cmd.exe and Windows batch files are totally insane! – mivk Oct 15 '11 at 10:54
  • 13
    NOTE TO SELF: The line "set NLM=^" **must** have nothing after the CARET and **must** have 2 blank lines after it. – mnemotronic Oct 24 '15 at 16:26
  • Unfortunately you cannot use it in a variable. For example, `set z=foo%NL%bar & echo %z%` will not give the expected results, it will only print `foo`. – Synetech Dec 21 '15 at 04:46
  • 4
    See my answer for a simpler method. `set nl=^&echo.`, then just `echo hello %nl% world` yields the same effect. – coltonon Jun 29 '16 at 05:37
  • I'm getting "There should be a newlineinserted here." – soulblazer Apr 12 '17 at 05:40
  • @Synetech You can indeed put a linebreak in a variable, using the first form. Try this out: `set A=first line^^^%NLM%%NLM%^%NLM%%NLM%second line` then `echo %A%` – rkagerer Oct 05 '19 at 22:26
  • @rkagerer, it works. Nice! (Too bad the command-line's escaping methods are so insane. :-\) – Synetech Oct 06 '19 at 23:10
101

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
macropas
  • 2,912
  • 3
  • 20
  • 23
  • 37
    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 04:58
  • 22
    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
  • @Aacini could you find some reference for this? I tested and it works which is fascinating but there must exist some designer memo, specification or at least manual that tells this story. – n611x007 Apr 18 '14 at 16:45
  • 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
  • 7
    @naxa - Nothing official, just collective knowledge derived by a few batch hackers. The goal is to have syntax that can be used to ECHO any value, including nothing, and never have to worry about getting anything other than what is expected. The best summary I know of is at http://www.dostips.com/forum/viewtopic.php?p=4554#p4554. It kind of leaves you hanging, but no one has ever come up with a scenario where `ECHO(` fails. – dbenham Apr 18 '14 at 17:20
  • @dbenham, The claim that `echo(` is the only version which works is incorrect. The author rules against `.`, `+`, `[`, and `]`, but there is still "`echo=`", "`echo,`", "`echo\ `", "`echo/ `" and "`echo:`" which he didn't address at all. – Pacerier Jul 29 '15 at 05:44
  • @Aacini, Why not `echo=`? Using `/` is misleading because `/` already has a meaning in cmd (used for representing flags). – Pacerier Jul 29 '15 at 05:45
  • 2
    @Pacerier: try `echo=/?` and `echo,/?` and you will see that both fails (because they do _not_ show "/?"). From the remaining three, `echo\/?` looks like an "escape" in other languages, and `echo:/?` may be mistaken for a label or drive. The form that looks "more standard" is `echo//?` _precisely_ because `/` is used for command options! – Aacini Jul 29 '15 at 10:06
  • 3
    @Pacerier - there are examples of failure for each of those suggestions. Carefully read http://www.dostips.com/forum/viewtopic.php?p=4554#p4554. – dbenham Jul 29 '15 at 12:23
  • 5
    @Pacerier there is enough _echo_ going on here for a standalone question. Perhaps something along the lines of "what characters can immediately follow `echo`, and what are their effects?" – matt wilkie Jul 31 '15 at 01:49
  • Can confirm `echo(` seems arbitrarily safe, even in deeply nested FOR loops which can be notorious for special-character weirdness handling. – kayleeFrye_onDeck Nov 06 '18 at 01:20
  • @dbenham when I use `echo(` it is not creating a newline, but rather this `ഠ嘊攀爀猀椀漀渀                    ഀ਀ 㤀㘀䌀㄀     㐀 㔀䘀   㔀㄀㘀㘀 ㄀㠀   ഀ਀`. – Cardinal System Feb 01 '20 at 18:40
74

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.

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 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., echo\ or echo: should be avoided, as they can be really slow, depending of the location where the script will be executed, like a network drive.

Community
  • 1
  • 1
jeb
  • 70,992
  • 15
  • 159
  • 202
  • 2
    If you will be passing the string with newlines as a parameter to a batch subroutine, e.g. `call :pause line1!\n!line2`, start the subroutine with `setlocal EnableDelayedExpansion` and end it with `setlocal DisableDelayedExpansion` to keep the newlines from being interpreted prematurely. – stevek_mcc Nov 08 '16 at 12:36
  • 2
    +1 for the cast of characters `echo,` `echo;` `echo(` `echo/` `echo+` `echo=` and the rogues gallery `echo.` ``echo\`` `echo:`. Now if you were to explain why in holy heck these work, I'd owe you a free beer. – Bob Stein Apr 07 '19 at 12:33
43

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

Outputs

hello
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.

Breakdown

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
coltonon
  • 776
  • 12
  • 30
21

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
Crend King
  • 3,525
  • 2
  • 30
  • 41
  • 8
    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 09 '12 at 15:27
  • 1
    `(echo hello && echo world) > temp` mentioned by Nathan J. Brauer in a comment to the (current) accepted answer works. – Gerold Broser Jun 09 '16 at 09:35
16

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

(echo a & echo. & echo b) > file_containing_multiple_lines.txt
test30
  • 2,920
  • 28
  • 24
15

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
10

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)
albert
  • 369
  • 3
  • 8
8

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 :)

Wayne Uroda
  • 4,598
  • 3
  • 28
  • 34
3

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:

TYPE file.txt | FIND "" /V > file_win.txt
del file.txt
rename file_win.txt file.txt
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
otaviodecampos
  • 1,011
  • 9
  • 8
3

To start a new line in batch, all you have to do is add "echo[", like so:

echo Hi!
echo[
echo Hello!
PryroTech
  • 503
  • 3
  • 12
  • 5
    It's slow and it fails when a file exists with the name `echo[.bat` – jeb Dec 23 '16 at 10:17
  • What? It should work instantly, and also just delete the echo[.bat file. – PryroTech Dec 23 '16 at 15:56
  • 2
    It is pretty much the exact same thing as echo. syntax. – PryroTech Dec 23 '16 at 15:57
  • 5
    Yes and `echo.` is a bad solution, too. – jeb Dec 24 '16 at 18:11
  • 1
    As can be seen in previous answers, this will try to locate a file on the path first, which causes it to be so slow. Same is true for `echo.`. Expecting users of your script to delete files on their system just because your script needs a newline doesn't sound like good design to me. – Abel Oct 29 '17 at 20:31
3

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:

@echo off
set input=%1
if defined input (
    set answer=Hi!\nWhy did you call me a %input%?
) else (
    set answer=Hi!\nHow are you?\nWe are friends, you know?\nYou can call me by name.
)

setlocal enableDelayedExpansion
set newline=^


rem Two empty lines above are essential
echo %answer:\n=!newline!%

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:

:mlecho
setlocal enableDelayedExpansion
set text=%*
set nl=^


echo %text:\n=!nl!%
goto:eof

Please note, that additional backslash won't prevent the script from parsing \n substring.

Tomator
  • 81
  • 8
  • 2
    You are essentially doing the same thing as Jeb's answer above. – Squashman Jun 08 '18 at 15:18
  • Answers I read above were very helpful but I needed to call a script od subroutine which will parse \n so I'd avoid implementing the trick everywhere. This needed to refine a solution and I shared this refinement. Hope it didn't harm any animal. – Tomator Jun 29 '18 at 08:36
  • This answer is useful, but since exclamation marks are interpreted, how can your argument display a litteral exclamation mark? I tried escaping them by prefixing one or two carets, but it did not work. – Florent Angly Jan 26 '21 at 14:24
  • @FlorentAngly, try this: `set ex=!` `setlocal enableDelayedExpansion` `echo shout!ex!` – Tomator Jan 27 '21 at 19:11
2

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 . . .
djangofan
  • 25,461
  • 54
  • 171
  • 262
NahuelGQ
  • 170
  • 1
  • 1
  • 9
  • 5
    -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
  • 2
    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 03 '13 at 15:48
1

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.

johan d
  • 2,672
  • 16
  • 26
1

simple

set nl=.
echo hello
echo%nl%
REM without space ^^^
echo World

Result:

hello
world
Pear
  • 326
  • 7
  • 2
    Where is the difference to [this 13 years old answer](https://stackoverflow.com/a/132804/463115)? Your answer is complex without necessity. And like the other *solution*, it doesn't answer the question how to do it with a single echo – jeb Apr 30 '21 at 11:55
0

why not use substring/replace space to echo;?

set "_line=hello world"
echo\%_line: =&echo;%
  • Results:
hello
world
  • Or, replace \n to echo;
set "_line=hello\nworld"
echo\%_line:\n=&echo;%
It Wasn't Me
  • 2,145
  • 3
  • 15
  • 22
-3

This solution works(Tested):

type nul | more /e /p

This converts isolated line feeds to carriage return line feed combination.

programmer365
  • 12,641
  • 3
  • 7
  • 28