views:

313

answers:

6

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.

A: 

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
Yeh, I had a go at this, it didn't work sadly.
Benj
+1  A: 

Ahaha,

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

echo hello&echo.&echo world

Produces:

hello

world

Benj
A: 

Or you can use '-e' option of echo command, like this:

echo -e 'Hello\nWorld'

Mihail Dimitrov
Like I say, it's easy on Unix (which you're referring to), but not so easy on Windows.
Benj
+4  A: 

After a little experimentation I discovered that it is possible to do it without issuing two separate echo commands as described in http://stackoverflow.com/questions/132799/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
Clifford
Funny, I thought I'd tried this, but I've just tried it again and it does indeed work.
Benj
+5  A: 

echo. is new line

Blind Trevor
A: 

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