如何从批处理文件输出中插入换行符?
我想做的事情是:
echo hello\nworld
这将输出:
hello
world
如何从批处理文件输出中插入换行符?
我想做的事情是:
echo hello\nworld
这将输出:
hello
world
当前回答
请注意,所有解决方案使用光标定位根据控制台虚拟终端序列,光标定位与:
Sequence | Code | Description | Behaviour |
---|---|---|---|
ESC [ <n> E | CNL | Cursor Next Line | Cursor down <n> lines from current position |
只有在没有到达控制台窗口底部时才能工作。
在底部没有空间来向下移动光标,所以它只是向左移动(与CRLF的CR),之前打印的行从它的开始被覆盖。
其他回答
要批量地开始新一行,你所要做的就是添加“echo[”,像这样:
echo Hi!
echo[
echo Hello!
有一个标准的功能echo:在cmd/bat-files中写入空行,这模拟了cmd-output中的新行:
@echo off
echo line1
echo:
echo line2
or
@echo line1 & echo: & echo line2
上面引用的cmd-file输出:
line1
line2
就像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语句之间的代码。
请注意,所有解决方案使用光标定位根据控制台虚拟终端序列,光标定位与:
Sequence | Code | Description | Behaviour |
---|---|---|---|
ESC [ <n> E | CNL | Cursor Next Line | Cursor down <n> lines from current position |
只有在没有到达控制台窗口底部时才能工作。
在底部没有空间来向下移动光标,所以它只是向左移动(与CRLF的CR),之前打印的行从它的开始被覆盖。