In bash, how can I convert a Unicode Codepoint [0-9A-F] into a printable character?

You can use bash's echo or /bin/echo from GNU coreutils in combination with iconv:

echo -ne '\x09\x65' | iconv -f utf-16be

By default iconv converts to your locales encoding. Perhaps more portable than relying on a specific shell or echo command is Perl. Most any UNIX system I am aware of while have Perl available and it even have several Windows ports.

perl -C -e 'print chr 0x0965'

Most of the time when I need to do this, I'm in an editor like Vim/GVim which has built-in support. While in insert mode, hit Ctrl-V followed by u, then type four hex characters. If you want a character beyond U+FFFF, use a capital U and type 8 hex characters. Vim also supports custom easy to make keymaps. It converts a series of characters to another symbol. For example, I have a keymap I developed called www, it converts TM to ™, (C) to ©, (R) to ®, and so on. I also have a keymap for Klingon for when that becomes necessary. I'm sure Emacs has something similar. If you are in a GTK+ app which includes GVim and GNOME Terminal, you can try Control-Shift-u followed by 4 hex characters to create a Unicode character. I'm sure KDE/Qt has something similar.

UPDATE: As of Bash 4.2, it seems to be a built in feature now:

echo $'\u0965'

UPDATE: Also, nowadays a Python example would probably be preferred to Perl. This works in both Python 2 and 3:

python -c 'print(u"\u0965")'

Bash 4.2 (released in 2011) added support for echo -e '\u0965', printf '\u0965', printf %b '\u0965' and echo $'\u0965' also work.

http://tiswww.case.edu/php/chet/bash/FAQ:

o   $'...', echo, and printf understand \uXXXX and \UXXXXXXXX escape sequences.

If you have GNU coreutils, try printf:

$ printf '\u0965\n'
॥

echo can do the job if your console is using UTF-8 and you have the UTF-8 encoding:

$ echo -e '\xE0\xA5\xA5'

You can find a table of Unicode to UTF-8 hex encodings here: http://www.utf8-chartable.de/. You can convert the Unicode code points to hex using a number of scripting languages. Here is an example using python:

python -c "print(unichr(int('0965', 16)).encode('utf-8').encode('hex'))"

The following is a Perl script that will convert arguments to the correct hex value (many unnecessary parenthesis here):

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Encode;

foreach (@ARGV) {
    say unpack('H*', encode('utf8', chr(hex($_))))
}

For instance,

./uni2utf 0965
e0a5a5

Of course, if you have Perl or Python you could also just use those to print the characters.

Tags:

Unicode

Bash