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

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


当前回答

我发现这个简单的单行批处理文件“EchoPart.bat”非常有用。

@echo | set /p=%*

然后,我可以在交互式CMD行中,或者作为快捷方式的一部分,写如下所示的内容。它带来了一些新的可能性。

echopart "Hello, " & echopart "and then " & echo Goodbye

如果你在批处理文件中使用它,文本可以从参数变量而不是不可变字符串中获得。例如:

@echopart Hello %* & @echo , how are you?

因此,在"SayHello.bat"中执行这一行允许:

甚至……

玩个游戏,玩得开心!

其他回答

DIY cw.exe(控制台编写)实用程序

如果你找不到现成的,你可以自己动手做。有了这个cw实用程序,你可以使用每一种字符。至少,我是这么想的。请对它进行压力测试,然后告诉我。

工具

你所需要的只是安装。net,这在当今非常普遍。

材料

输入/复制粘贴了一些字符。

步骤

用以下内容创建.bat文件。

/* >nul 2>&1

@echo off
setlocal

set exe=cw
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\*csc.exe"') do set "csc=%%v"

"%csc%" -nologo -out:"%exe%.exe" "%~f0"

endlocal
exit /b %errorlevel%

*/

using System;

namespace cw {
    class Program {
        static void Main() {
            var exe = Environment.GetCommandLineArgs()[0];
            var rawCmd = Environment.CommandLine;
            var line = rawCmd.Remove(rawCmd.IndexOf(exe),exe.Length).TrimStart('"');
            line = line.Length < 2 ? "\r" : line.Substring(2) ;
            Console.Write(line);
        }
    }
}

运行它。 现在您有了一个很棒的4KB实用程序,可以删除.bat。

或者,您可以在任何批处理中插入此代码作为子例程,将生成的.exe发送到%temp%,在批处理中使用它,并在完成时删除它。

如何使用

如果你想写一些没有换行的东西: 任何你想要的,即使是"",但记住转义^|,^^,^&等,除非双引号,如"| ^& "。

如果你想要一个回车(回到行首),运行just 连续波

所以试试从命令行:

for /l %a in (1,1,1000) do @(cw ^|&cw&cw /&cw&cw -&cw&cw \&cw)

简单的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

可以使用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命令不识别"!"字符,并打印一个点来代替它。我希望你不要用感叹号“!”;)

使用Bill Stewart的“Shell脚本工具包”中的EchoX.EXE 如何在Windows Cmd脚本中抑制换行:

@Echo Off
Rem Print three Echos in one line of output
EchoX -n "Part 1 - "
EchoX -n "Part 2 - "
EchoX    "Part 3"
Rem

给:

Part 1 - Part 2 - Part 3
{empty line}
d:\Prompt>

这种用法的帮助是:

Usage: echox [-n] message
  -n       Do not skip to the next line.
  message  The text to be displayed.

该实用程序小于48K,应该位于Path中。它可以做的更多事情:-打印文本而不移动到下一行-打印文本在一定宽度内向左、中间或右边对齐-打印文本与制表符,换行和返回-打印文本在前景和背景颜色

该工具包还包括12个很棒的脚本编写技巧。 下载页面还包含其他三个有用的工具包。

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.