The decimal character equivalent in a linux shell script

I need to convert the numbers: decimal 13 and decimal 10 to their equivalent characters in a bash shell script. Generated characters will return a carriage and a line.

Any idea how this can be done?

+4
source share
4 answers

I hate to give this answer, but in bash you can do:

  nl = $ '\ 12'

so $ nl is a line with one new line. (octal 12) So you can fill out a decimal value with something like:

  N = 10
 N8 = $ (printf% o $ N)
 eval nl = "\ $ '\\ $ N8'"
+1
source

You can use printf twice, once to convert to octal, another to convert to ascii

 A=10 printf \\$(printf '%03o' "$A") 

Proof

 $ A=10;printf \\$(printf '%03o' "$A") | od -c 0000000 \n 0000001 
+1
source

This is probably not the easiest solution, but the first thing that comes to mind is to use printf. Unfortunately, printf requires octal values, so you need to translate. To print A (decimal value of ascii 65), you can use dc to calculate the octal number and do the following:

  $ A = 65
 $ printf \\ $ (echo 8o $ {A} p | dc)

If you want to use perl:

  $ perl -e "print chr $ A" # print character whose decimal value is stored in shell variable A
 $ perl -e 'print chr 10' # print a carriage return
0
source

Maybe the regex for you is something else:

 msg="13 ways to leave your lover is a top 10 hit for 1310 days" stp1=${msg//10/\\n} echo -e ${stp1//13/\\r} 

result:

  ways to leave your lover is a top hit for days 
0
source

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


All Articles