How to get the one character’s next character in ASCII table in Bash?

Posted on In QA, Tutorial

How to get the one character’s next character in ASCII table in Bash? For example, if I have ‘a’ in variable i, how to get ‘b’?

First, we need to get the integer value for the character.

char='b'
charint=$(printf "%d" "'$char'")

Then, we increase the integer by one

let charint=$charint+1

Last, we can get the new character by converting the integer to the character

newchar=$(printf "\x$(printf %x $charint)")

Here, we use the inner printf to convert the integer to hexadecimal value and the outer printf to print the character from its hexadecimal value.

The result is as follows.

$ echo $newchar
c