Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print unicode character

Tags:

bash

printf

With Bash, you can use printf with hex codes

\xHH   byte with hexadecimal value HH (1 to 2 digits)
\uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)

example

$ printf '\x26'
&

However the Unicode example does not print as expected

$ printf '\u0026'
\u0026

My Bash version

$ echo $BASH_VERSION
4.1.10(4)-release
like image 227
Zombo Avatar asked Apr 15 '13 17:04

Zombo


People also ask

How do I print Unicode?

Use the "\u" escape sequence to print Unicode characters In a string, place "\u" before four hexadecimal digits that represent a Unicode code point.

How do I show Unicode in Python?

To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123 in your string. In Python 2. x, you also need to prefix the string literal with 'u'.

What is Unicode character in Python?

Python's string type uses the Unicode Standard for representing characters, which lets Python programs work with all these different possible characters. Unicode (https://www.unicode.org/) is a specification that aims to list every character used by human languages and give each character its own unique code.

How do you write a character in Unicode?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.


1 Answers

What version of bash are you using (echo $BASH_VERSION or bash --version)? The Unicode escapes work in bash 4.2, but not 3.2.48. I suspect support was added in bash 4.0.

Update: I can confirm it does not work in bash 4.1.2. This functionality appears to have been added in bash 4.2.

Update 2: From the release notes for bash 4.2:

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

like image 162
chepner Avatar answered Oct 19 '22 13:10

chepner