Bash script to get ASCII values for alphabet

asciibashbash-script

How do I get the ASCII value of the alphabet?

For example, 97 for a?

Best Answer

Define these two functions (usually available in other languages):

chr() {
  [ "$1" -lt 256 ] || return 1
  printf "\\$(printf '%03o' "$1")"
}

ord() {
  LC_CTYPE=C printf '%d' "'$1"
}

Usage:

chr 65
A

ord A
65
Related Question