你如何在 Bash 中回显 4 位 Unicode 字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/602912/
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
How do you echo a 4-digit Unicode character in Bash?
提问by masukomi
I'd like to add the Unicode skull and crossbones to my shell prompt (specifically the 'SKULL AND CROSSBONES' (U+2620)), but I can't figure out the magic incantation to make echo spit it, or any other, 4-digit Unicode character. Two-digit one's are easy. For example, echo -e "\x55", .
我想将 Unicode 骷髅和交叉骨添加到我的 shell 提示中(特别是“SKULL AND CROSSBONES”(U+2620)),但我无法弄清楚让 echo 吐出它的魔法咒语,或任何其他, 4 位 Unicode 字符。两位数的很容易。例如, echo -e "\x55", 。
In addition to the answers below it should be noted that, obviously, your terminal needs to support Unicode for the output to be what you expect. gnome-terminal does a good job of this, but it isn't necessarily turned on by default.
除了下面的答案,应该注意的是,显然,您的终端需要支持 Unicode 才能使输出符合您的预期。gnome-terminal 在这方面做得很好,但它不一定默认打开。
On macOS's Terminal app Go to Preferences-> Encodings and choose Unicode (UTF-8).
在 macOS 的终端应用程序上,转到首选项-> 编码并选择 Unicode (UTF-8)。
回答by vartec
In UTF-8 it's actually 6 digits (or 3 bytes).
在 UTF-8 中,它实际上是 6 位(或 3 个字节)。
$ printf '\xE2\x98\xA0'
?
To check how it's encoded by the console, use hexdump:
要检查控制台如何对其进行编码,请使用 hexdump:
$ printf ? | hexdump
0000000 98e2 00a0
0000003
回答by Juliano
% echo -e '\u2620' # \u takes four hexadecimal digits
?
% echo -e '\U0001f602' # \U takes eight hexadecimal digits
This works in Zsh (I've checked version 4.3) and in Bash 4.2 or newer.
这适用于 Zsh(我检查过 4.3 版)和 Bash 4.2 或更新版本。
回答by RobM
So long as your text-editors can cope with Unicode (presumably encoded in UTF-8) you can enter the Unicode code-point directly.
只要您的文本编辑器可以处理 Unicode(大概以 UTF-8 编码),您就可以直接输入 Unicode 代码点。
For instance, in the Vimtext-editor you would enter insert mode and press Ctrl+ V+ Uand then the code-point number as a 4-digit hexadecimal number (pad with zeros if necessary). So you would type Ctrl+ V+ U2620. See: What is the easiest way to insert Unicode characters into a document?
例如,在Vim文本编辑器中,您将进入插入模式并按Ctrl+ V+ U,然后将代码点编号作为 4 位十六进制数(如有必要,用零填充)。所以你会输入Ctrl+ V+ U2620。请参阅:将 Unicode 字符插入文档的最简单方法是什么?
At a terminal running Bash you would type CTRL+SHIFT+Uand type in the hexadecimal code-point of the character you want. During input your cursor should show an underlined u
. The first non-digit you type ends input, and renders the character. So you could be able to print U+2620 in Bash using the following:
在运行 Bash 的终端上,您可以输入CTRL+ SHIFT+U并输入所需字符的十六进制代码点。在输入过程中,您的光标应显示一个带下划线的u
。您键入的第一个非数字结束输入,并呈现字符。因此,您可以使用以下命令在 Bash 中打印 U+2620:
echoCTRL+SHIFT+U2620ENTERENTER
echoCTRL+ SHIFT+U2620ENTERENTER
(The first enter ends Unicode input, and the second runs the echo
command.)
(第一个输入结束 Unicode 输入,第二个输入运行echo
命令。)
Credit: Ask Ubuntu SE
信用:询问 Ubuntu SE
回答by Orwellophile
Here's a fully internal Bash implementation, no forking, unlimited size of Unicode characters.
这是一个完全内部的 Bash 实现,没有分叉,Unicode 字符的大小不受限制。
fast_chr() {
local __octal
local __char
printf -v __octal '%03o'
printf -v __char \$__octal
REPLY=$__char
}
function unichr {
local c= # Ordinal of char
local l=0 # Byte ctr
local o=63 # Ceiling
local p=128 # Accum. bits
local s='' # Output string
(( c < 0x80 )) && { fast_chr "$c"; echo -n "$REPLY"; return; }
while (( c > o )); do
fast_chr $(( t = 0x80 | c & 0x3f ))
s="$REPLY$s"
(( c >>= 6, l++, p += o+1, o>>=1 ))
done
fast_chr $(( t = p | c ))
echo -n "$REPLY$s"
}
## test harness
for (( i=0x2500; i<0x2600; i++ )); do
unichr $i
done
Output was:
输出是:
─━│┃┄┅┆┇┈┉┊┋┌┍┎┏
┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟
┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯
┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿
╀╁╂╃╄╅╆╇╈╉╊╋????
═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟
╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯
╰╱╲╳????????????
?▁▂▃▄▅▆▇█▉▊▋▌▍▎▏
???▓▔▕??????????
■□??????????????
??▲△????????▼▽??
??????◆◇???○??◎●
????????????????
??◢◣◤◥??????????
????????????????
回答by Joachim Sauer
Just put "?" in your shell script. In the correct locale and on a Unicode-enabled console it'll print just fine:
只需输入“?” 在你的 shell 脚本中。在正确的语言环境和支持 Unicode 的控制台上,它会打印得很好:
$ echo ?
?
$
An ugly "workaround" would be to output the UTF-8 sequence, but that also depends on the encoding used:
一个丑陋的“解决方法”是输出 UTF-8 序列,但这也取决于使用的编码:
$ echo -e '\xE2\x98\xA0'
?
$
回答by David King
Quick one-liner to convert UTF-8 characters into their 3-byte format:
将 UTF-8 字符转换为 3 字节格式的快速单行:
var="$(echo -n '?' | od -An -tx1)"; printf '\x%s' ${var^^}; echo
回答by Metal3d
I'm using this:
我正在使用这个:
$ echo -e '\u2620'
?
This is pretty easier than searching a hex representation... I'm using this in my shell scripts. That works on gnome-term and urxvt AFAIK.
这比搜索十六进制表示要容易得多......我在我的 shell 脚本中使用它。这适用于 gnome-term 和 urxvt AFAIK。
回答by cms
You may need to encode the code point as octal in order for prompt expansion to correctly decode it.
您可能需要将代码点编码为八进制,以便快速扩展以正确解码它。
U+2620 encoded as UTF-8 is E2 98 A0.
U+2620 编码为 UTF-8 是 E2 98 A0。
So in Bash,
所以在 Bash 中,
export PS1="200"
will make your shell prompt into skull and bones.
将使您的 shell 提示进入头骨和骨骼。
回答by user2622016
In bash to print a Unicode character to output use \x,\u or \U (first for 2 digit hex, second for 4 digit hex, third for any length)
在 bash 中打印 Unicode 字符以输出使用 \x,\u 或 \U(第一个用于 2 位十六进制,第二个用于 4 位十六进制,第三个用于任何长度)
echo -e '\U1f602'
I you want to assign it to a variable use $'...' syntax
我想将它分配给变量使用 $'...' 语法
x=$'\U1f602'
echo $x
回答by user2622016
Any of these three commands will print the character you want in a console, provided the console do accept UTF-8characters (most current ones do):
这三个命令中的任何一个都将在控制台中打印您想要的字符,前提是控制台接受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) ?
After, you could copy and paste the actual glyph (image, character) to any (UTF-8 enabled) text editor.
之后,您可以将实际字形(图像、字符)复制并粘贴到任何(支持 UTF-8 的)文本编辑器。
If you need to see how such Unicode Code Point is encoded in UTF-8, use xxd (much better hex viewer than od):
如果您需要查看此类 Unicode 代码点是如何以 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
Or, in HEX to avoid errors: 0xE2 0x98 0xA0. That is, the values between the space (HEX 20) and the Line-Feed (Hex 0A).
或者,在十六进制中避免错误:0xE2 0x98 0xA0。即,空格 (HEX 20) 和换行符 (Hex 0A) 之间的值。
If you want a deep dive into converting numbers to chars: look hereto see an article from Greg's wiki (BashFAQ) about ASCII encoding in Bash!
如果您想深入了解将数字转换为字符:请看这里,查看 Greg 的 wiki (BashFAQ) 中有关 Bash 中 ASCII 编码的文章!