如何从批处理文件输出中插入换行符?

我想做的事情是:

echo hello\nworld

这将输出:

hello
world

当前回答

Use:

echo hello
echo:
echo world

其他回答

如果你需要把结果放到一个文件中,你可以使用:

(echo a & echo: & echo b) > file_containing_multiple_lines.txt

注意,这不会在控制台工作,因为它将模拟一个escape键和清除行。

使用这段代码,将<ESC>替换为0x1b转义字符或使用这个Pastebin链接:

:: Replace <ESC> with the 0x1b escape character or copy from this Pastebin:
:: https://pastebin.com/xLWKTQZQ

echo Hello<ESC>[Eworld!

:: OR

set "\n=<ESC>[E"
echo Hello%\n%world!

就像Grimtron建议的那样-这里有一个简单的例子来定义它:

@echo off
set newline=^& echo.
echo hello %newline%world

输出

C:\>test.bat
hello
world

在这里,创建一个.bat文件,其中包含以下内容:

@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

您应该会看到如下输出:

There should be a newline
inserted here.

Press any key to continue . . .

显然,您只需要REM语句之间的代码。

可以使用@echo (@echo +[空间]+[不安全空间])

注:不安全空间可用Alt+0160获取

希望能有所帮助。

嗯,你是对的,我需要它在一个Makefile中,它在那里工作得很好。我想我的答案不适合批处理文件…我的坏。