Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

bash - Print unicode character

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...