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

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


当前回答

SET /P中被剥离空白的解决方案:

窍门是你可以在DOS的文本编辑器EDIT中调用的退格字符。要在EDIT中创建它,请按ctrl +ctrlH。 我想把它粘贴在这里,但是这个网页不能显示它。它在记事本上是可见的(它很奇怪,就像一个小的黑色矩形,中间有一个白圈)

你可以这样写:

<nul set /p=.9    Hello everyone

点可以是任何字符,它只是告诉SET /P文本从这里开始,在空格之前,而不是在“Hello”处。 “9”是一个退格字符的表示,我不能在这里显示。你必须用它来代替9,它就会删除",之后你会得到:

    Hello Everyone

而不是:

Hello Everyone

我希望这对你们有帮助

其他回答

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

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

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

使用set和/p参数可以不带换行符进行回显:

C:\> echo Hello World
Hello World

C:\> echo|set /p="Hello World"
Hello World
C:\>

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%

使用:echo | set /p=或<NUL set /p=都可以抑制换行符。

然而,当编写更高级的脚本时,检查ERRORLEVEL变得很重要,因为设置set /p=而不指定变量名将把ERRORLEVEL设置为1,这可能是非常危险的。

一个更好的方法是使用一个虚拟变量名,就像这样: echo | set /p dummyName=Hello World

这将产生你想要的东西,没有任何偷偷摸摸的东西在后台,因为我必须找到艰难的方法,但这只适用于管道版本;<NUL set /p dummyName=Hello仍然会将ERRORLEVEL提升为1。