打印unicode字符

9 bash printf

使用Bash,您可以使用printf十六进制代码

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

$ printf '\x26'
&
Run Code Online (Sandbox Code Playgroud)

但是Unicode示例不按预期打印

$ printf '\u0026'
\u0026
Run Code Online (Sandbox Code Playgroud)

我的Bash版本

$ echo $BASH_VERSION
4.1.10(4)-release
Run Code Online (Sandbox Code Playgroud)

che*_*ner 7

bash您使用的是什么版本(echo $BASH_VERSIONbash --version)?Unicode转义在bash4.2中工作,但不在3.2.48.我怀疑bash4.0 中添加了支持.

更新:我可以确认它在bash4.1.2中不起作用.此功能似乎已添加到bash4.2中.

更新2:从bash 4.2发行说明:

d. $'...',echoprintf理解\uXXXX\UXXXXXXXX逃避序列.