How can you echo a newline in batch files?

2019-01-01 06:12发布

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

18条回答
人间绝色
2楼-- · 2019-01-01 06:54

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.

查看更多
零度萤火
3楼-- · 2019-01-01 06:54

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
查看更多
梦醉为红颜
4楼-- · 2019-01-01 06:56

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
查看更多
心情的温度
5楼-- · 2019-01-01 06:57

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)
查看更多
余欢
6楼-- · 2019-01-01 06:58

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

(echo a & echo. & echo b) > file_containing_multiple_lines.txt
查看更多
后来的你喜欢了谁
7楼-- · 2019-01-01 07:00

I can't make it any simpler than:

echo echo hello^&echo world^&pause>silly.bat                       
call silly.bat
查看更多
登录 后发表回答