bash 如何在当前终端的会话中设置语言环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43953328/
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 16:09:03  来源:igfitidea点击:

How to set locale in the current terminal's session?

bashencodingterminallocalerxvt

提问by Timur Fayzrakhmanov

I'm trying to change encoding in the urxvt current session by changing LANGvariable. Howerever, it seems like it doesn't apply immediately. Here is what I do:

我正在尝试通过更改LANG变量来更改 urxvt 当前会话中的编码。但是,它似乎不会立即适用。这是我所做的:

Available locales:

可用的语言环境:

$ locale -a
C
en_US.utf8
POSIX
ru_RU.koi8r
ru_RU.utf8

Before setting new locale:

在设置新语言环境之前:

$ echo "а" | od -t x1
0000000 d0 b0 0a # good! UTF-8
#       | a ||NL|

After:

后:

$ export LANG=ru_RU.KOI8-R
$ echo "а" | od -t x1
0000000 d0 b0 0a # hm..expect 'c1 0a'

Fork new urxvt instance by running $ urxvt &and finally get what I want:

通过运行 fork 新的 urxvt 实例$ urxvt &并最终得到我想要的:

$ echo "а" | od -t x1
0000000 c1 0a

Why doesn't LANGchange the behavior in the first place?

为什么不LANG首先改变行为?

回答by Thomas Dickey

There are two factors:

有两个因素:

  • you may be using a shell with a built-in echo (and have not informed the shell that you are changing the locale)
  • LANGis not the first environment variable checked. According to locale(7), LC_ALLand LC_CTYPEwould be checked first:
  • 您可能正在使用带有内置 echo 的外壳(并且没有通知外壳您正在更改区域设置)
  • LANG不是第一个检查的环境变量。根据locale(7),LC_ALL并且LC_CTYPE将首先检查:
       If the second argument to setlocale(3) is an empty string, "", for
       the default locale, it is determined using the following steps:

       1.     If there is a non-null environment variable LC_ALL, the value
              of LC_ALL is used.

       2.     If an environment variable with the same name as one of the
              categories above exists and is non-null, its value is used for
              that category.

       3.     If there is a non-null environment variable LANG, the value of
              LANG is used.

For the latter, look at the output from the localecommand, which lists all of the environment variables which would be used:

对于后者,请查看locale命令的输出,其中列出了将使用的所有环境变量:

$ export LANG=ru_RU.KOI8-R
$ locale
LANG=ru_RU.KOI8-R
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

Just changing LANGshould not change the other variables, but changing LC_ALLgenerally does that.

只是改变LANG不应该改变其他变量,但改变LC_ALL通常会这样做。

$ export LC_ALL=ru_RU.KOI8-R
$ locale
LANG=ru_RU.KOI8-R
LANGUAGE=
LC_CTYPE="ru_RU.KOI8-R"
LC_NUMERIC="ru_RU.KOI8-R"
LC_TIME="ru_RU.KOI8-R"
LC_COLLATE="ru_RU.KOI8-R"
LC_MONETARY="ru_RU.KOI8-R"
LC_MESSAGES="ru_RU.KOI8-R"
LC_PAPER="ru_RU.KOI8-R"
LC_NAME="ru_RU.KOI8-R"
LC_ADDRESS="ru_RU.KOI8-R"
LC_TELEPHONE="ru_RU.KOI8-R"
LC_MEASUREMENT="ru_RU.KOI8-R"
LC_IDENTIFICATION="ru_RU.KOI8-R"
LC_ALL=ru_RU.KOI8-R