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

你说的曾经没有我的故事 提交于 2020-05-25 07:10:07

问题


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

回答1:


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.




回答2:


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



回答3:


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'



来源:https://stackoverflow.com/questions/36019010/how-to-get-windows-cmd-echo-to-echo-exactly-one-single-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!