如何在批处理文件中回显换行符?

时间:2020-03-06 14:42:58  来源:igfitidea点击:

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

我想做类似的事情:

> echo hello \ nworld

哪个会输出:

hello 

  world

解决方案

使用:

echo hello
echo.
echo world

回声你好&echo.world

这意味着我们可以将"&echo。"定义为换行符" \ n"的常量。

在这里,创建一个.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 hello > temp
echo world >> temp

就像格里姆创(Grimtron)建议的那样,这里是一个定义它的简单示例:

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

输出

C:\>test.bat
hello
world

这对我有用,不需要延迟扩展:

@echo off
(
echo ^<html^> 
echo ^<body^>
echo Hello
echo ^</body^>
echo ^</html^>
)
pause

它这样写输出:

<html>
<body>
Hello
</body>
</html>
Press any key to continue . . .

cmd / bat文件中有一个标准功能echo:来写空行,它在cmd输出中模拟了新行:

@echo off
@echo line1
@echo:
@echo line2

以上cmd文件引用的输出:

line1

line2