在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?

其思想是在循环中的同一行上写入。


当前回答

你可以从gnuwin32 (coreutils包)中使用“tr”删除换行符

@echo off
set L=First line
echo %L% | tr -d "\r\n"
echo Second line
pause

顺便说一下,如果您正在编写大量脚本,gnuwin32是一个金矿。

其他回答

我相信没有这样的选择。或者你可以试试这个

set text=Hello
set text=%text% world
echo %text%

Late answer here, but for anyone who needs to write special characters to a single line who find dbenham's answer to be about 80 lines too long and whose scripts may break (perhaps due to user-input) under the limitations of simply using set /p, it's probably easiest to just to pair your .bat or .cmd with a compiled C++ or C-language executable and then just cout or printf the characters. This will also allow you to easily write multiple times to one line if you're showing a sort of progress bar or something using characters, as OP apparently was.

从这里

<nul set /p =Testing testing

同时也从空间的使用开始呼应

echo.Message goes here

我发现这个简单的单行批处理文件“EchoPart.bat”非常有用。

@echo | set /p=%*

然后,我可以在交互式CMD行中,或者作为快捷方式的一部分,写如下所示的内容。它带来了一些新的可能性。

echopart "Hello, " & echopart "and then " & echo Goodbye

如果你在批处理文件中使用它,文本可以从参数变量而不是不可变字符串中获得。例如:

@echopart Hello %* & @echo , how are you?

因此,在"SayHello.bat"中执行这一行允许:

甚至……

玩个游戏,玩得开心!

这里是另一个方法,它使用Powershell Write-Host,它有一个-NoNewLine参数,结合start /b,它从批处理中提供了相同的功能。

NoNewLines.cmd

@ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - ';Write-Host -NoNewLine 'Result 2 - ';Write-Host -NoNewLine 'Result 3 - '"
PAUSE

输出

Result 1 - Result 2 - Result 3 - Press any key to continue . . .

下面这个略有不同,并不完全像OP想要的那样工作,但很有趣,因为每个结果都覆盖了模拟计数器的前一个结果。

@ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 2 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 3 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 4 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 5 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 6 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 7 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 8 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 9 - '"
PAUSE