Symbol value:
c="a"
To convert a character to its ASCII value:
v=$(printf %d "'$c")
The value you want to add to this ASCII value:
add=1
To change its ASCII value by adding $ add to it:
((v+=add))
To convert the result to char:
perl -X -e "printf('The character is %c\n', $v);"
I used -X to disable all warnings
You can combine all this into one line and put the result in vairable $ r:
c="a"; add=1; r=$(perl -X -e "printf('%c', $(($add+$(printf %d "'$c"))));")
you can print the result:
echo "$r"
You can make a function to return the result:
achar () { c="$1"; add=$2 printf "$(perl -X -e "printf('%c', $(($add+$(printf %d "'$c"))));")" }
you can use the function:
x=$(achar "a" 1)
or you can do a loop:
array=( akmo ) for l in "${array[@]}" do echo "$l" is followed by $(achar "$l" 1) done
user37421
source share