我想将Unicode头骨和交叉骨头添加到我的shell提示符(特别是' skull and crossbones ' (U+2620)),但我不知道让echo吐出它的魔法咒语,或任何其他4位Unicode字符。两位数的数字很简单。例如echo -e "\x55",。

除了下面的答案之外,还应该注意到,很明显,您的终端需要支持Unicode以使输出符合您的期望。Gnome-terminal在这方面做得很好,但它在默认情况下不一定是打开的。

在macOS的终端应用程序中,选择首选项->编码,并选择Unicode (UTF-8)。


当前回答

在Bash中:

UnicodePointToUtf8()
{
    local x="$1"               # ok if '0x2620'
    x=${x/\\u/0x}              # '\u2620' -> '0x2620'
    x=${x/U+/0x}; x=${x/u+/0x} # 'U-2620' -> '0x2620'
    x=$((x)) # from hex to decimal
    local y=$x n=0
    [ $x -ge 0 ] || return 1
    while [ $y -gt 0 ]; do y=$((y>>1)); n=$((n+1)); done
    if [ $n -le 7 ]; then       # 7
        y=$x
    elif [ $n -le 11 ]; then    # 5+6
        y=" $(( ((x>> 6)&0x1F)+0xC0 )) \
            $(( (x&0x3F)+0x80 ))" 
    elif [ $n -le 16 ]; then    # 4+6+6
        y=" $(( ((x>>12)&0x0F)+0xE0 )) \
            $(( ((x>> 6)&0x3F)+0x80 )) \
            $(( (x&0x3F)+0x80 ))"
    else                        # 3+6+6+6
        y=" $(( ((x>>18)&0x07)+0xF0 )) \
            $(( ((x>>12)&0x3F)+0x80 )) \
            $(( ((x>> 6)&0x3F)+0x80 )) \
            $(( (x&0x3F)+0x80 ))"
    fi
    printf -v y '\\x%x' $y
    echo -n -e $y
}

# test
for (( i=0x2500; i<0x2600; i++ )); do
    UnicodePointToUtf8 $i
    [ "$(( i+1 & 0x1f ))" != 0 ] || echo ""
done
x='U+2620'
echo "$x -> $(UnicodePointToUtf8 $x)"

输出:

─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟
┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿
╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟
╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿
▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟
■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱▲△▴▵▶▷▸▹►▻▼▽▾▿
◀◁◂◃◄◅◆◇◈◉◊○◌◍◎●◐◑◒◓◔◕◖◗◘◙◚◛◜◝◞◟
◠◡◢◣◤◥◦◧◨◩◪◫◬◭◮◯◰◱◲◳◴◵◶◷◸◹◺◻◼◽◾◿
U+2620 -> ☠

其他回答

快速一行代码将UTF-8字符转换为3字节格式:

var="$(echo -n '☠' | od -An -tx1)"; printf '\\x%s' ${var^^}; echo

or

echo -n '☠' | od -An -tx1 | sed 's/ /\\x/g'  

两者的输出都是\xE2\x98\xA0,所以你可以反过来写:

echo $'\xe2\x98\xa0'   # ☠

在UTF-8中,它实际上是6个数字(或3个字节)。

$ printf '\xE2\x98\xA0'
☠

要检查它是如何被控制台编码的,使用hexdump:

$ printf ☠ | hexdump
0000000 98e2 00a0                              
0000003

这三个命令中的任何一个都可以在控制台中打印你想要的字符,前提是控制台接受UTF-8字符(大多数当前的都接受):

echo -e "SKULL AND CROSSBONES (U+2620) \U02620"
echo $'SKULL AND CROSSBONES (U+2620) \U02620'
printf "%b" "SKULL AND CROSSBONES (U+2620) \U02620\n"

SKULL AND CROSSBONES (U+2620) ☠

之后,您可以将实际的字形(图像、字符)复制并粘贴到任何文本编辑器(启用UTF-8)。

如果你需要看到这样的Unicode Code Point是如何用UTF-8编码的,使用xxd(比od更好的十六进制查看器):

echo $'(U+2620) \U02620' | xxd
0000000: 2855 2b32 3632 3029 20e2 98a0 0a         (U+2620) ....

That means that the UTF8 encoding is: e2 98 a0

或者,在HEX中避免错误:0xE2 0x98 0xA0。也就是说,空格(HEX 20)和换行(HEX 0A)之间的值。

如果您想深入了解数字到字符的转换:请查看Greg的wiki (BashFAQ)中关于Bash中的ASCII编码的文章!

对不起,我又提了这个老问题。但是在使用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).

简单地使用Python2/3一行代码:

$ python -c 'print u"\u2620"'    # python2
$ python3 -c 'print(u"\u2620")'  # python3

结果: