How to make Windows CMD ECHO display exactly one character?

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

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

For example, this will affect 'a' with the new line 'windows' ( \r\n ):

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

And this trick (also in the docs now) doesn't hear anything:

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

So, I would expect this to only reflect "a":

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

But it adds extra space at the end.

Is it possible to just make one character?

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

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

And are there any tricks to get a more accurate one, such as UNIX ECHO with -n (no new line) and -e ( use backslash interpretation ), so I could with similar conclusions:

 >unix_echo -n -e "a\n" | od -A x -t x1z -v - 000000 61 0a >a.< 000002 
+5
source share
2 answers

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

 set /P "name=Enter your name: " 

This command displays the message and places the cursor after it. We can make good use of this behavior to show a β€œhint” that does not end in CR + LF, and then complete the dummy input redirecting Stdin to NUL. In this case, the variable name is not required:

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

Thus, to output only one character, use this:

 set /P "=a" < NUL 

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

+4
source

To use the newline character (\ n), carriage return character (\ r) or backspace (\ b) in the output, you can create auxiliary variables. These variables should only be used with extension delay (or you should know what you are doing).

 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 repeat one space (or more) without a new line, the set / p trick does not work, but you can create another workaround by creating a temporary file with one 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 
+2
source

Source: https://habr.com/ru/post/1245108/


All Articles