Windows batch: echo without new line
Using set
and the /p
parameter you can echo without newline:
C:\> echo Hello WorldHello WorldC:\> echo|set /p="Hello World"Hello WorldC:\>
Using: echo | set /p=
or <NUL set /p=
will both work to suppress the newline.
However, this can be very dangerous when writing more advanced scripts when checking the ERRORLEVEL becomes important as setting set /p=
without specifying a variable name will set the ERRORLEVEL to 1.
A better approach would be to just use a dummy variable name like so:echo | set /p dummyName=Hello World
This will produce exactly what you want without any sneaky stuff going on in the background as I had to find out the hard way, but this only works with the piped version; <NUL set /p dummyName=Hello
will still raise the ERRORLEVEL to 1.
The simple SET /P method has limitations that vary slightly between Windows versions.
Leading quotes may be stripped
Leading white space may be stripped
Leading
=
causes a syntax error.
See http://www.dostips.com/forum/viewtopic.php?f=3&t=4209 for more information.
jeb posted a clever solution that solves most of the problems at Output text without linefeed, even with leading space or = I've refined the method so that it can safely print absolutely any valid batch string without the new line, on any version of Windows from XP onward. Note that the :writeInitialize
method contains a string literal that may not post well to the site. A remark is included that describes what the character sequence should be.
The :write
and :writeVar
methods are optimized such that only strings containing troublesome leading characters are written using my modified version of jeb's COPY method. Non-troublesome strings are written using the simpler and faster SET /P method.
@echo offsetlocal disableDelayedExpansioncall :writeInitializecall :write "=hello"call :write " world!%$write.sub%OK!"echo(setlocal enableDelayedExpansionset lf=^set "str= hello!lf!world^!!!$write.sub!hello!lf!world"echo(echo str=!str!echo(call :write "str="call :writeVar strecho(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 disableDelayedExpansionset "str=%~1"call :writeVar strexit /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 /bsetlocal enableDelayedExpansionif not defined $write.sub call :writeInitializeset $write.special=1if "!%~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 >nultype "%$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 >nulfor /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