Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I get the ASCII value of the alphabet?

For example, 97 for a?

share|improve this question

5 Answers 5

up vote 33 down vote accepted

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"


chr 65

ord A
share|improve this answer
@dmsk80: +1. For others like me who think they spot a typo: "'A" is correct whereas if you use "A" it will say : A: invalid number . It seems it's done on printf side (ie, in the shell, "'A" is indeed 2 chars, a ' and a A. Those are passed to printf. And in the printf context, it is converted to the ascii value of A, (and is finally printed as a decimal thanks to the '%d'. Use 'Ox%x' to show it in hexa or '0%o' to have it in octal)) –  Olivier Dulac Sep 26 '13 at 11:05
-1 for not explaining how it works... joking :D, but seriously what do these printf "\\$(printf '%03o' "$1")", '%03o', LC_CTYPE=C and the single quote in "'$1" do? –  razzak Dec 4 '14 at 19:42

You can see the entire set with:

$ man ascii

You'll get tables in octal, hex, and decimal.

share|improve this answer
There's also an ascii package for debian-based distros, but (at least now) the question is tagged as bash, so these wouldn't help the OP. In fact, it's installed on my system and all I get from man ascii is its man page. –  Joe Sep 27 '13 at 21:47

If you want to extend it to UTF-8 characters:

$ perl -CA -le 'print ord shift' 😈

$ perl -CS -le 'print chr shift' 128520

With bash, ksh or zsh builtins:

$ printf "\U$(printf %08x 128520)\n"
share|improve this answer
Did you intend to put a square box character or else then the original char is not being displayed in the post and is being replaced by a square box character. –  mtk Oct 2 '13 at 7:11
@mtk, You need a browser that displays UTF-8 and a font that has that 128520 character. –  Stéphane Chazelas Oct 2 '13 at 7:13
I am on Latest Chrome, and don't think that it doesn't support UTF-8. Would like to know what browser you are on? –  mtk Oct 2 '13 at 8:53
@mtk, iceweasel on Debian sid. The font as confirmed by iceweasel's web console is "DejaVu Sans" and I've got ttf-dejavu ttf-dejavu-core ttf-dejavu-extra packages installed which come from Debian with upstream at dejavu-fonts.org –  Stéphane Chazelas Oct 2 '13 at 9:10

This works well,

echo "A" | tr -d "\n" | od -An -t uC
share|improve this answer
Can you maybe add a small explanation? –  Bernhard Sep 26 '13 at 12:04
tr to remove "\n" (new line ) from the input. od is used to -t dC is to print in decimal character. –  Saravanan Sep 26 '13 at 12:42
echo -n suppresses trailing newline eliminating the need for tr -d "\n" –  Gowtham Sep 26 '13 at 16:59
@Gowtham, only with some implementations of echo, not in Unix compliant echos for instance. printf %s A would be the portable one. –  Stéphane Chazelas Sep 27 '13 at 8:44

I'm going for the simple (and elegant?) Bash solution:

for i in {a..z}; do echo $(printf "%s %d" "$i" "'$i"); done

For in a script you can use the following:

AscValue=`printf "%d" "'$CharValue"

Notice the single quote before the CharValue. It is obligated...

share|improve this answer
How is your answer different from dsmsk80's answer? –  Bernhard Sep 26 '13 at 14:35
My interpretation of the question is "how to get the ASCII values for the values of the alphabet". Not how to define a function to retrieve the ASCII value for one character. So my first answer is a short one-line command to get the ASCII values for the alphabet. –  phulstaert Sep 27 '13 at 7:26
I get your point, but I still think the bottom line of both answer is printf "%d". –  Bernhard Sep 27 '13 at 9:11
I agree this is a crucial part of the process to get to the result, yet i didn't wanted to make the assumption that xmpirate knew about the "for i in" and the use of a range. If he wanted a list, this could be a real time-saver ;-). Also, future readers might find my additions helpful. –  phulstaert Sep 27 '13 at 9:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.