19

Is it possible to put a new line character in an echo line in a batch file?

Basically I want to be able to do the equivalent of:

echo Hello\nWorld

You can do this easily enough in Linux, but I can't work out how to do it in Windows.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Benj
  • 29,382
  • 17
  • 72
  • 119

7 Answers7

36

echo. prints an empty line.

Example:

echo Hello
echo.
echo world

prints

Hello

world
Nulano
  • 691
  • 6
  • 22
Blind Trevor
  • 670
  • 1
  • 9
  • 26
  • 7
    `echo.` is ~20 times slower than `echo(` and it fails completly when a file exists with the name `echo` without extension. So `echo(` is safer and faster – jeb Sep 15 '14 at 14:21
  • 4
    `echo(` doesn't work in DOS 6.22. (Although that's probably a minimal audience at this point.) – Jim Davis Aug 09 '15 at 07:05
  • @JimDavis +1 for DOS 6.22 compatibility – Hannes Schneidermayer Oct 07 '15 at 14:31
  • @jeb where did you get the performance information? What about `echo[`? – Synetech Dec 21 '15 at 05:03
  • @Synetech I measured it with some thousand of echo's. I didn't measured `echo[` but as it fails also with a file named `echo[.bat` it should be as slow as `echo.` Infos about the echo variations at [ECHO. FAILS to give text or blank line](http://www.dostips.com/forum/viewtopic.php?p=4554#p4554) – jeb Dec 21 '15 at 08:56
21

It can be solved with a single echo.

You need a newline character \n for this. There are multiple ways to get a new line into the echo

1) This sample use the multiline caret to add a newline into the command,
the empty line is required

echo Hello^

world

2) The next solution creates first a variable which contains one single line feed character.

set \n=^


rem ** Two empty lines are required

Or create the new line with a slightly modified version

(set \n=^
%=DONT REMOVE THIS=%
)

And use this character with delayed expansion

setlocal EnableDelayedExpansion
echo Hello!\n!world

To use a line feed character with the percent expansion you need to create a more complex sequence

echo Hello^%\n%%\n%world

Or you can use the New line hack

REM Creating a Newline variable (the two blank lines are required!)
set \n=^


set NL=^^^%\n%%\n%^%\n%%\n%
REM Example Usage:
echo There should be a newline%NL%inserted here.

But only the delayed expansion of the newline works reliable, also inside of quotes.

Community
  • 1
  • 1
jeb
  • 70,992
  • 15
  • 159
  • 202
  • +1 For bringing this question back from the grave ;-) – Benj Nov 03 '11 at 11:11
  • +1, great to have the possibility to use line-feed in a variable with immediate (percent) expansion; however, Is there a way to expand a carriage-return character in the same manner without delayed expansion? – aschipfl Aug 03 '16 at 23:48
  • @aschipfl No, it's not possible, as all carriage returns are removed just after the percent expansion phase and there is no way to escape them – jeb Aug 04 '16 at 09:16
  • Caret (`^`) does not work in MS-DOS, at least FreeDOS. Separate `echo` commands, one per line, seem to be required. – mcandre Aug 11 '19 at 19:59
  • Is it somehow possible to assign just **carriage return character** `\r` into a variable? – Ωmega Feb 24 '20 at 18:32
  • @Ωmega Yes, try a look at [Batch countdown timer that doesn't clear rest of output?](https://stackoverflow.com/a/56331593/463115) or [How to get Windows CMD ECHO to echo exactly one single character?](https://stackoverflow.com/a/36068719/463115) – jeb Feb 24 '20 at 20:17
  • Thanks, I use very similar solution, but same as yours it does NOT allow to print `CR` character alone/separately, just with some non-white text in front of it. Any workaround? – Ωmega Feb 24 '20 at 20:20
  • @Ωmega But that is not related to this question here – jeb Feb 24 '20 at 20:53
13

After a little experimentation I discovered that it is possible to do it without issuing two separate echo commands as described in How can you echo a newline in batch files?. However to make it work you will need a text editor that does not translate CR to CR+LF.

Type:

@echo First Line

then with NumLock on, hold down the ALT key and type 10 on the numeric keypad before releasing ALT (you must use the numeric keypad, not the top-row number keys). This will insert a CR character. Then type the second line. Depending on your editor and how it handles CR compared with CR+LF you may get:

@echo First Line◙Second Line

or

@echo First Line
Second Line

This works from the command line and will work in a batch file so long as the text editor does not translate CR to CR+LF (which Windows/DOS editors do unless you configure them not to). If the CR is converted to CR+LF, or if you use just LF, the second line is interpreted as a new command.

However, I cannot see why this would be preferable over simply:

@echo First Line
@echo Second Line
Community
  • 1
  • 1
Clifford
  • 76,825
  • 12
  • 79
  • 145
  • Funny, I thought I'd tried this, but I've just tried it again and it does indeed work. – Benj Nov 12 '09 at 11:19
  • I don't know which OS you tried, but your solution can't work, as a _raw_ CR is completely removed by the batch parser. In the best case a CR would move the cursor back to the line begin, not to the next line – jeb Nov 05 '14 at 13:22
  • @jeb : That would depend entirely on how the command shell handles the CR. I tested it in Windows cmd.exe (since that is how the question is tagged), and as commented by Benj, it worked for him. cmd.exe presumably performs the translation to CR+LF. Most terminal emulators will support similar translation by configuration, but you are right that the technique is not generally applicable - but that was not how the question was presented. I remain bemused regarding why anyone would want to do this, but there it is! – Clifford Nov 05 '14 at 19:44
  • I assumed also windows(batch), but I just retested it and it doesn't work, I tested with a single CR(13) and also with a single LF(10) character. As I said I can't believe that your solution could work. [How does the CMD.EXE parse scripts?](http://stackoverflow.com/a/4095133/463115) Points 1.5 and 2 – jeb Nov 05 '14 at 20:50
  • Works fine on the CLI [6.3.9600] even if it doesn't work in a batch file. `I remain bemused regarding why anyone would want to do this` - I sometimes use this to make the %PATH% variable human readable: `echo %path:;=◙%` – Amit Naidu Oct 07 '16 at 21:05
6

Ahaha,

I think I've worked out something close enough...

echo hello&echo.&echo world

Produces:

hello

world

Benj
  • 29,382
  • 17
  • 72
  • 119
2

echo. or echo(

will do the blank new line. Hope this is helpful.

Reu Roo
  • 133
  • 1
  • 10
  • 1
    But this does'nt answer the question how to add a new line between two texts. – jeb Jul 10 '15 at 05:21
0

I found this very informative, so wanted to post a better example using the answers provided

This provides a nicely formatted usage message

if "%1" == """" goto usage

:usage
 echo  USAGE: %0 [Set properties using -D flag] [Ant Task to Run]  &
 echo.                                                             &
 echo        Availble Command line properties                      &
 echo        --------------------------------                      &  
...
Wiretap
  • 721
  • 2
  • 9
  • 13
-2

I think it is not possible. You could only try an ascii-character to this: http://www.asciitable.com/ But this will perhaps crash your batch-file.

martin
  • 2,797
  • 3
  • 23
  • 42