Q. I want to convert a given decimal number to ascii value, How can I do that using a shell script?

Below script will give you the ascii value corresponding number

#!/bin/bash

printf “Please enter a value between 32 to 127(Range of ascii values): “
read VAL1
printf “$(printf “%o” $VAL1)n”

Let me explain this script. The script will take input from user in to VAL1 variable and pass it to printf command. printf “%o” $VAL1 will try to change the decimal value to octal. then this octal value is once again feed to the second printf as printf “octvaluen”. printf command have one option what ever followed after will be treated as octal which helps use to convert to ascii values.   Want to print all the ascii values? Execute below script to do that   #!/bin/bash
for i in {32..127}
do
printf “$(printf “%o” $i)n”
done

or just one single liner which will work in bash version 4 and above.    printf  “$(printf “%on” {32..127})”   Please comment your thoughts on this.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.