How to get Windows CMD ECHO to echo exactly one single character?

The set /P command is used to prompt the user and accept an input. For example:

set /P "name=Enter your name: "

This command show the prompt message and place the cursor after it. We may make good use of this behavior to show a "prompt" that does not end in CR+LF, and then complete the dummy input redirecting Stdin to NUL. In this case, the variabe name is not needed:

set /P "=Text with no CR+LF at end" < NUL

This way, to output just one character, use this:

set /P "=a" < NUL

Note that set /P command omit any leading space from the prompt message. This means that it is not possible to use this method to show only spaces.


To use a newline(\n), carriage return (\r) or backspace (\b) character in an output you could create helper variables.
This variables should be used only with delayed expansion (or you should know what you do).

setlocal EnableDelayedExpansion
(
set \n=^
%=DO NOT MODIFY THIS LINE=%
)
for /F "tokens=1,2 delims=# " %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "\b=%%a"
)
for /f %%a in ('copy /Z "%~dpf0" nul') do (
  set "\r=%%a"
)

echo Line1!\n!Line2

<nul set /p ".=Line1!\n!Line2 without"
echo  end

echo 12345!\b!*
echo 12345!\r!*

To echo a single space (or more) without a newline the set/p trick doesn't work, but you can create another workaround by building a temporary file with a single space.

setlocal EnableDelayedExpansion

(set LF=^
%=EMPTY=%
)
call :createSpaceFile
type spaceFile.tmp
echo After the space
exit /b

:createSpaceFile
<nul set /p ".=X!LF! " > spaceFile1.tmp
findstr /V "X" spaceFile1.tmp > spaceFile.tmp
exit /b