Equivalent to echo-e on Windows?

Is there a Linux equivalent of "echo -e" in windows, so I can use "echo -e \ xnnn" to print a character whose ASCII code is the hexadecimal value of nnn?

+8
windows echo batch-file
source share
4 answers

There is no equivalent, but you can write your own function.

I would divide the problem into two parts.

  • Convert hexadecimal to decimal.
  • Convert decimal to ASCII.

Convert from hexadecimal to decimal simple to

set "myHex=4A" set /a decimal=0x%myHex% 

Converting a number to ASCII is more complicated, it depends on the required ASCII range. If you need a range from 32 (space) to 126 '~', you can use the variable =ExitCodeAscii

 set "decimal=%1" cmd /c exit /b %decimal% echo "%=ExitCodeAscii%" 

If you also need special characters such as CR , LF , ESC , DEL , they can be created using a clean batch.

If you need more than you could use vbscript.

forfiles method

A more general way is to use the forfiles command ( Dostips: create almost any character ... )

echo e.bat

 @echo off set "arg1=%~1" set "arg1=%arg1:\x=0x%" forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%arg1%" 

You can call via echo-e.bat "Hello\x01\x02\x03" and you will receive Hello☺☻β™₯ .

+11
source share

There is a batch file symbol manipulation library written by Dave Benham called CharLib . This library, among other things, is able to convert hex to ascii, for all characters in the range 1..255 (this cannot be 0 β†’ NUL).

For more information, see Can a character be converted to its ASCII value? at DosTips.com

If you just need to use one problem character (e.g. control code) in your batch script, then this is an invaluable resource for cutting and pasting.

eg. to print a CR character (without LF) in a script package ...

 @echo off setlocal enableextensions enabledelayedexpansion for /f %%a in ('copy /Z "%~dpf0" nul') do set "ASCII_13=%%a" echo 1234567890!ASCII_13!xxxxxx 

will finish printing "xxxxxx7890" on the console as expected.

+5
source share

Here is a simple hybrid package / JScript file that can be used to echo the results of any valid JScript expressions, including using \ xnn. It supports all byte codes from \ x00 to \ xFF.

A package takes one argument, enclosed in double quotes. The argument must be a valid JScript expression. Double quotes are not part of the expression. JScript strings inside an expression must be enclosed in single quotes.

By default, a new line is not printed at the end of the result. The / N option is used to add a new line at the end of the result.

If used in a batch file, then, of course, the script should be called using CALL.

jEval.bat

 @if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment ::: Batch part :::: @echo off cscript //nologo //e:JScript "%~f0" %* exit /b *** JScript part ***/ if (WScript.Arguments.Named.Exists("n")) { WScript.StdOut.WriteLine(eval(WScript.Arguments.Unnamed(0))); } else { WScript.StdOut.Write(eval(WScript.Arguments.Unnamed(0))); } 

Here is an example call that uses \ xnn to enter a new line in the output file, and also contains floating point math.

 >jEval " 'Hello world!\x0a4/5=' + 4/5 " /n Hello world! 4/5=0.8 > 
+2
source share

Adding to what @jeb says you can recreate

 echo -n -e "win\x01\x02\x03\x04" > file.exe 

from

 @echo off setlocal set LF=^ set "arg1=%~1" set "arg1=%arg1:\x=0x%" for /f eol^=^%LF%%LF%^ delims^= %%A in ( 'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo|set /p=%arg1%"' ) do if "%~2" neq "" (set %~2=%%A) else echo|set /p=%%A 

which becomes:

 echone.bat "win\x01\x02\x03\x04" > file.exe 
+1
source share

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


All Articles