在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
在Windows批处理中,Linux shell命令echo -n抑制输出末尾的换行符是什么?
其思想是在循环中的同一行上写入。
当前回答
我根据@arnep的想法做了一个函数:
echo|set /p="Hello World"
下面就是:
:SL (sameline)
echo|set /p=%1
exit /b
使用call:SL "Hello There" 我知道这没什么特别的,但我花了很长时间才想出来,所以我想我应该把它贴在这里。
其他回答
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.
你可以从gnuwin32 (coreutils包)中使用“tr”删除换行符
@echo off
set L=First line
echo %L% | tr -d "\r\n"
echo Second line
pause
顺便说一下,如果您正在编写大量脚本,gnuwin32是一个金矿。
示例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
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#
我发现这个简单的单行批处理文件“EchoPart.bat”非常有用。
@echo | set /p=%*
然后,我可以在交互式CMD行中,或者作为快捷方式的一部分,写如下所示的内容。它带来了一些新的可能性。
echopart "Hello, " & echopart "and then " & echo Goodbye
如果你在批处理文件中使用它,文本可以从参数变量而不是不可变字符串中获得。例如:
@echopart Hello %* & @echo , how are you?
因此,在"SayHello.bat"中执行这一行允许:
甚至……
玩个游戏,玩得开心!