bash 打印 unicode 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16021137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:11:37  来源:igfitidea点击:

Print unicode character

bashprintf

提问by Steven Penny

With Bash, you can use printfwith hex codes

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

\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

但是 Unicode 示例没有按预期打印

$ printf '\u0026'
\u0026

My Bash version

我的 Bash 版本

$ echo $BASH_VERSION
4.1.10(4)-release

采纳答案by chepner

What version of bashare you using (echo $BASH_VERSIONor bash --version)? The Unicode escapes work in bash4.2, but not 3.2.48. I suspect support was added in bash4.0.

bash您使用的是哪个版本的(echo $BASH_VERSIONbash --version)?Unicode 转义在bash4.2 中有效,但在 3.2.48 中无效。我怀疑在bash4.0中添加了支持。

Update: I can confirm it does not work in bash4.1.2. This functionality appears to have been added in bash4.2.

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

Update 2: From the release notes for bash 4.2:

更新 2:来自bash 4.2发行说明

d. $'...', echo, and printfunderstand \uXXXXand \UXXXXXXXXescape sequences.

d. $'...', echo, 以及printf理解\uXXXX\UXXXXXXXX转义序列。