Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The (unofficial) documentation for the Windows Internal CMD ECHO shows some interesting tricks in it. However, I have not yet found a way to echo a single character.

Quick note, od used below, is from a Git (or Gow) installation

For instance, this echo's the 'a' with a 'windows' newline (\r\n):

>echo a| od -A x -t x1z -v -
000000 61 0d 0a                                         >a..<
000003

And this trick (also in the docs now) echo's nothing:

><nul (set/p _any_variable=)| od -A x -t x1z -v -
000000

So I would expect this to echo just the 'a':

><nul (set/p _any_variable=a)| od -A x -t x1z -v -
000000 61 20                                            >a <
000002  

But it adds the extra space at the end.

Is it possible to just do a single character?

@Aacini answered (the first question) correctly in a comment below, but in case he does not create an answer, here it is:

>set /P "=a" < NUL | od -A x -t x1z -v -
000000 61                                               >a<
000001

And are there any tricks to get more precise like the UNIX echo with a -n (no new line) and -e (use backslash interpretation) so I could similar outputs to this:

>unix_echo -n -e "a\n" |  od -A x -t x1z -v -
000000 61 0a                                            >a.<
000002
like image 952
Joshua Ball Avatar asked Mar 15 '16 18:03

Joshua Ball


People also ask

How do I echo text in CMD?

To display the command prompt, type echo on. If used in a batch file, echo on and echo off don't affect the setting at the command prompt. To prevent echoing a particular command in a batch file, insert an @ sign in front of the command.

What does @echo off do?

The ECHO-ON and ECHO-OFF commands are used to enable and disable the echoing, or displaying on the screen, of characters entered at the keyboard. If echoing is disabled, input will not appear on the terminal screen as it is typed. By default, echoing is enabled.

How do you echo an empty line?

To create a blank line in a batch file, add an open bracket or period immediately after the echo command with no space, as shown below. Adding @echo off at the beginning of the batch file turns off the echo and does not show each of the commands. @echo off echo There will be a blank line below. echo.


2 Answers

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.

like image 138
Aacini Avatar answered Oct 22 '22 15:10

Aacini


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
like image 3
jeb Avatar answered Oct 22 '22 15:10

jeb