我想将Unicode头骨和交叉骨头添加到我的shell提示符(特别是' skull and crossbones ' (U+2620)),但我不知道让echo吐出它的魔法咒语,或任何其他4位Unicode字符。两位数的数字很简单。例如echo -e "\x55",。
除了下面的答案之外,还应该注意到,很明显,您的终端需要支持Unicode以使输出符合您的期望。Gnome-terminal在这方面做得很好,但它在默认情况下不一定是打开的。
在macOS的终端应用程序中,选择首选项->编码,并选择Unicode (UTF-8)。
只要您的文本编辑器能够处理Unicode(假定以UTF-8编码),您就可以直接输入Unicode代码点。
例如,在Vim文本编辑器中,您可以进入插入模式并按Ctrl + V + U,然后按4位十六进制数(必要时可以用零填充)。输入Ctrl + V + U 2 6 20。参见:在文档中插入Unicode字符的最简单方法是什么?
在运行Bash的终端上,您可以键入CTRL+SHIFT+U,并键入所需字符的十六进制码位。在输入过程中,你的光标应该显示一个带下划线的u。你输入的第一个非数字结束输入,并呈现字符。所以你可以在Bash中使用以下方法打印U+2620:
echo CTRL + SHIFT + U2620ENTERENTER
(第一个输入结束Unicode输入,第二个执行echo命令。)
来源:Ubuntu SE
对不起,我又提了这个老问题。但是在使用bash时,有一种非常简单的方法可以从纯ASCII输入创建Unicode码点,甚至根本没有分叉:
unicode() { local -n a="$1"; local c; printf -vc '\\U%08x' "$2"; printf -va "$c"; }
unicodes() { local a c; for a; do printf -vc '\\U%08x' "$a"; printf "$c"; done; };
如下所示使用它来定义某些代码点
unicode crossbones 0x2620
echo "$crossbones"
或者将前65536个unicode码点转储到stdout(在我的机器上耗时不到2秒。额外的空格是为了防止某些字符由于shell的monospace字体而相互流入):
for a in {0..65535}; do unicodes "$a"; printf ' '; done
或者讲一个非常典型的父母的故事(这需要Unicode 2010):
unicodes 0x1F6BC 32 43 32 0x1F62D 32 32 43 32 0x1F37C 32 61 32 0x263A 32 32 43 32 0x1F4A9 10
解释:
printf '\UXXXXXXXX' prints out any Unicode character
printf '\\U%08x' number prints \UXXXXXXXX with the number converted to Hex, this then is fed to another printf to actually print out the Unicode character
printf recognizes octal (0oct), hex (0xHEX) and decimal (0 or numbers starting with 1 to 9) as numbers, so you can choose whichever representation fits best
printf -v var .. gathers the output of printf into a variable, without fork (which tremendously speeds up things)
local variable is there to not pollute the global namespace
local -n var=other aliases var to other, such that assignment to var alters other. One interesting part here is, that var is part of the local namespace, while other is part of the global namespace.
Please note that there is no such thing as local or global namespace in bash. Variables are kept in the environment, and such are always global. Local just puts away the current value and restores it when the function is left again. Other functions called from within the function with local will still see the "local" value. This is a fundamentally different concept than all the normal scoping rules found in other languages (and what bash does is very powerful but can lead to errors if you are a programmer who is not aware of that).