在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.

其他回答

从这里

<nul set /p =Testing testing

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

echo.Message goes here

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

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

你可以这样写:

<nul set /p=.9    Hello everyone

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

    Hello Everyone

而不是:

Hello Everyone

我希望这对你们有帮助

简单的SET /P方法在不同的Windows版本之间有一些限制。

前导引号可能被删除 前导空白可能被剥离 Leading =导致语法错误。

更多信息请参见http://www.dostips.com/forum/viewtopic.php?f=3&t=4209。

jeb发布了一个聪明的解决方案,解决了输出文本没有换行的大部分问题,甚至有前导空格或=我已经改进了方法,以便它可以安全地打印任何有效的批处理字符串,没有新行,在任何版本的Windows XP开始。注意:writeInitialize方法包含一个字符串字面值,可能不能很好地发布到站点。包括一个注释,描述字符序列应该是什么。

:write和:writeVar方法经过优化,只有包含麻烦的前导字符的字符串才会使用我修改过的jeb的COPY方法来编写。不麻烦的字符串是使用更简单和更快的SET /P方法编写的。

@echo off
setlocal disableDelayedExpansion
call :writeInitialize
call :write "=hello"
call :write " world!%$write.sub%OK!"
echo(
setlocal enableDelayedExpansion
set lf=^


set "str= hello!lf!world^!!!$write.sub!hello!lf!world"
echo(
echo str=!str!
echo(
call :write "str="
call :writeVar str
echo(
exit /b

:write  Str
::
:: Write the literal string Str to stdout without a terminating
:: carriage return or line feed. Enclosing quotes are stripped.
::
:: This routine works by calling :writeVar
::
setlocal disableDelayedExpansion
set "str=%~1"
call :writeVar str
exit /b


:writeVar  StrVar
::
:: Writes the value of variable StrVar to stdout without a terminating
:: carriage return or line feed.
::
:: The routine relies on variables defined by :writeInitialize. If the
:: variables are not yet defined, then it calls :writeInitialize to
:: temporarily define them. Performance can be improved by explicitly
:: calling :writeInitialize once before the first call to :writeVar
::
if not defined %~1 exit /b
setlocal enableDelayedExpansion
if not defined $write.sub call :writeInitialize
set $write.special=1
if "!%~1:~0,1!" equ "^!" set "$write.special="
for /f delims^=^ eol^= %%A in ("!%~1:~0,1!") do (
  if "%%A" neq "=" if "!$write.problemChars:%%A=!" equ "!$write.problemChars!" set "$write.special="
)
if not defined $write.special (
  <nul set /p "=!%~1!"
  exit /b
)
>"%$write.temp%_1.txt" (echo !str!!$write.sub!)
copy "%$write.temp%_1.txt" /a "%$write.temp%_2.txt" /b >nul
type "%$write.temp%_2.txt"
del "%$write.temp%_1.txt" "%$write.temp%_2.txt"
set "str2=!str:*%$write.sub%=%$write.sub%!"
if "!str2!" neq "!str!" <nul set /p "=!str2!"
exit /b


:writeInitialize
::
:: Defines 3 variables needed by the :write and :writeVar routines
::
::   $write.temp - specifies a base path for temporary files
::
::   $write.sub  - contains the SUB character, also known as <CTRL-Z> or 0x1A
::
::   $write.problemChars - list of characters that cause problems for SET /P
::      <carriageReturn> <formFeed> <space> <tab> <0xFF> <equal> <quote>
::      Note that <lineFeed> and <equal> also causes problems, but are handled elsewhere
::
set "$write.temp=%temp%\writeTemp%random%"
copy nul "%$write.temp%.txt" /a >nul
for /f "usebackq" %%A in ("%$write.temp%.txt") do set "$write.sub=%%A"
del "%$write.temp%.txt"
for /f %%A in ('copy /z "%~f0" nul') do for /f %%B in ('cls') do (
  set "$write.problemChars=%%A%%B    ""
  REM the characters after %%B above should be <space> <tab> <0xFF>
)
exit /b

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

echo|set /p="Hello World"

下面就是:

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

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

也许这就是你要找的,这是一个老派的剧本……: P

set nl=^& echo. 
echo %nl%The%nl%new%nl%line%nl%is%nl%not%nl%apparent%nl%throughout%nl%text%nl%
echo only in prompt.
pause

或者您可能试图替换当前行,而不是写入新的行? 您可以通过删除“。”符号后的“%bs%”和在“示例消息”后的“%bs%”来进行实验。

for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "bs=%%a"
<nul set /p=.%bs%         Example message         %bs%
pause

我发现这真的很有趣,因为它使用变量的目的不是它想要做的事情。如你所见,“%bs%”表示退格。第二个“%bs%”使用退格符在“示例消息”之后添加空格,分隔“Pause命令的输出”,而不是在“示例消息”之后实际添加一个可见字符。然而,使用常规百分号也可以做到这一点。