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

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


当前回答

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.

其他回答

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

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

我根据@arnep的想法做了一个函数:

echo|set /p="Hello World"

下面就是:

:SL (sameline)
echo|set /p=%1
exit /b

使用call:SL "Hello There" 我知道这没什么特别的,但我花了很长时间才想出来,所以我想我应该把它贴在这里。

jscript:

@if (@X)==(@Y) @end /*
    @cscript //E:JScript //nologo "%~nx0" %*
    @exit /b %errorlevel%
*/if(WScript.Arguments.Count()>0) WScript.StdOut.Write(WScript.Arguments.Item(0));

如果它叫write.bat,你可以这样测试它:

call write.bat string & echo _Another_String_

如果你想使用powershell,但有cmd定义的变量,你可以使用:

set str=_My_StrinG_
powershell "Write-Host -NoNewline ""%str%""""  & echo #Another#STRING#

回显带有空格且没有换行符

如Pedro之前所述,没有新行和前面有空格的echo有效(假设“9”是一个真正的[BackSpace])。

<nul set /p=.9    Hello everyone

我在Windows 10的新主机上遇到了一些问题,但我还是通过以下方式解决了问题。 在CMD中输入:

echo .◘>bs.txt

我按[Alt] +[8]键实现了“▲” (实际符号可能因代码页而异)。

然后使用Notepad.exe将结果从“bs.txt”复制到需要的地方就很容易了。

@echo off
<nul set /p "_s=.◘    Hello everyone"
echo: here

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

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

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