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

Viewed 2653

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
3 Answers

echo writes the given argument plus 0d0a (CRLF).

To get one byte (character) from echo you'd need 2 backspace characters followed by character you want to echo.

0x08 (backspace char) can be extracted from prompt command.
set on the other hand can write one character (byte) to a file:
(set /p = a)<nul>one.txt echoes exactly one character 'a' to a file 'one.txt'

Related