在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
当前回答
示例1:它工作并生成退出代码= 0。这很好。 注意“。”,直接在echo之后。
C:\Users\phife.狗\ gitrepos \ 1 # \ repo_abc \脚本 @echo。| set /p JUNK_VAR=这是一个像Linux echo一样显示的消息-n将显示它…&回显%ERRORLEVEL%
这是一个像Linux echo -n一样显示的消息…0
示例2:这可以工作,但产生退出代码= 1。这很糟糕。 请注意回声后面缺少“。”。这似乎就是不同之处。
C:\Users\phife.狗\ gitrepos \ 1 # \ repo_abc \脚本 @echo | set /p JUNK_VAR=这是一个像Linux echo -n会显示它的消息…&回显%ERRORLEVEL%
这是一个像Linux echo -n一样显示的消息…1
其他回答
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 /p命令禁用新行。set /p命令不识别空格,因此可以使用点和退格字符使其识别空格。您还可以使用变量作为内存,并将想要打印的内容存储在其中,这样您就可以打印变量而不是语句。例如:
@echo off
setlocal enabledelayedexpansion
for /f %%a in ('"prompt $H & for %%b in (1) do rem"') do (set "bs=%%a")
cls
set "var=Hello World! :)"
set "x=0"
:loop
set "display=!var:~%x%,1!"
<nul set /p "print=.%bs%%display%"
ping -n 1 localhost >nul
set /a "x=%x% + 1"
if "!var:~%x%,1!" == "" goto end
goto loop
:end
echo.
pause
exit
通过这种方式,您可以打印任何内容而无需新行。我已经使程序打印字符一个接一个,但你也可以使用文字,而不是通过改变循环字符。
在上面的例子中,我使用了" enabledelayeexpand ",所以set /p命令不识别"!"字符,并打印一个点来代替它。我希望你不要用感叹号“!”;)
示例1:它工作并生成退出代码= 0。这很好。 注意“。”,直接在echo之后。
C:\Users\phife.狗\ gitrepos \ 1 # \ repo_abc \脚本 @echo。| set /p JUNK_VAR=这是一个像Linux echo一样显示的消息-n将显示它…&回显%ERRORLEVEL%
这是一个像Linux echo -n一样显示的消息…0
示例2:这可以工作,但产生退出代码= 1。这很糟糕。 请注意回声后面缺少“。”。这似乎就是不同之处。
C:\Users\phife.狗\ gitrepos \ 1 # \ repo_abc \脚本 @echo | set /p JUNK_VAR=这是一个像Linux echo -n会显示它的消息…&回显%ERRORLEVEL%
这是一个像Linux echo -n一样显示的消息…1
从这里
<nul set /p =Testing testing
同时也从空间的使用开始呼应
echo.Message goes here
这里是另一个方法,它使用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