shell script - is there any way converting number to char?

declare -i i=97
c=$(printf \\$(printf '%03o' $i))
echo "char:" $c

#!/bin/bash
# chr() - converts decimal value to its ASCII character representation
# ord() - converts ASCII character to its decimal value

chr() {
  printf \\$(printf '%03o' $1)
}

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

ord A
echo
chr 65
echo

Edit:

As you see ord() is a little tricky -- putting a single quote in front of an integer.

The Single Unix Specification: "If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote."

(Taken from http://mywiki.wooledge.org/BashFAQ/071).

See man printf(1p).

Tags:

Shell

Bash