开发者

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

This question already has answers here: How can I echo a newline in a batch file? (23 answers) Closed 7 years ago.

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.


echo. prints an empty line.

Example:

echo Hello
echo.
echo world

prints

Hello

world


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.


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


Ahaha,

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

echo hello&echo.&echo world

Produces:

hello

world


echo. or echo(

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜