如何获取终端的字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5306153/
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 to get terminal's Character Encoding
提问by Liang Wul
Now I change my gnome-terminal's character encoding to "GBK" (default it is UTF-8), but how can I get the value(character encoding) in my Linux?
现在我将 gnome 终端的字符编码更改为“GBK”(默认为 UTF-8),但是如何在 Linux 中获取值(字符编码)?
回答by Valdis
The terminal uses environment variablesto determine which character setto use, therefore you can determine it by looking at those variables:
终端使用环境变量来确定要使用的字符集,因此您可以通过查看这些变量来确定:
echo $LC_CTYPE
or
或者
echo $LANG
回答by Moreno
Check encoding and language:
检查编码和语言:
$ echo $LC_CTYPE
ISO-8859-1
$ echo $LANG
pt_BR
Get all languages:
获取所有语言:
$ locale -a
Change to pt_PT.utf8:
更改为 pt_PT.utf8:
$ export LC_ALL=pt_PT.utf8
$ export LANG="$LC_ALL"
回答by nyzm
localecommand with no arguments will print the values of all of the relevant environment variables except for LANGUAGE.
locale不带参数的命令将打印除 LANGUAGE 之外的所有相关环境变量的值。
For current encoding:
对于当前编码:
locale charmap
For available locales:
对于可用的语言环境:
locale -a
For available encodings:
对于可用的编码:
locale -m
回答by pythonator
To my knowledge, no.
据我所知,没有。
Circumstantial indications from $LC_CTYPE, localeand such might seem alluring, but these are completely separated from the encoding the terminal application (actually an emulator) happens to be using when displaying characters on the screen.
从间接指征$LC_CTYPE,locale并且这样的似乎诱人的,但这些都完全从编码终端应用程序(实际上是一个仿真器)分离发生在屏幕上显示字符时要使用。
They only way to detect encoding for sure is to output something only present in the encoding, e.g. ?, take a screenshot, analyze that image and check if the output character is correct.
他们确定检测编码的唯一方法是输出仅存在于编码中的内容,例如?,截取屏幕截图,分析该图像并检查输出字符是否正确。
So no, it's not possible, sadly.
所以不,很遗憾,这是不可能的。
回答by Martin Thoma
If you have Python:
如果你有 Python:
python -c "import sys; print(sys.stdout.encoding)"

