在很多的shell腳本中,我們經常會看見某一句命令的前面有一句“LC_ALL=C”
SAR_CMD="LC_ALL=C sar -u -b 1 5 | grep -i average "
這到底是什么意思?
LC_ALL=C 是為了去除所有本地化的設置,讓命令能正確執行。
---------------------------------
在Linux中通過locale來設置程序運行的不同語言環境,locale由ANSI C提供支持。locale的命名規則為<語言>_<地區>.<字符集編碼>,如zh_CN.UTF-8,zh代表中文,CN代表大陸地區,UTF-8表示字符集。
在locale環境中,有一組變量,代表國際化環境中的不同設置:
1. LC_COLLATE
定義該環境的排序和比較規則
2. LC_CTYPE
用於字符分類和字符串處理,控制所有字符的處理方式,包括字符編碼,字符是單字節還是多字節,如何打印等。是最重要的一個環境變量。
3. LC_MONETARY
貨幣格式
4. LC_NUMERIC
非貨幣的數字顯示格式
5. LC_TIME
時間和日期格式
6. LC_MESSAGES
提示信息的語言。另外還有一個LANGUAGE參數,它與LC_MESSAGES相似,但如果該參數一旦設置,則LC_MESSAGES參數就會失效。LANGUAGE參數可同時設置多種語言信息,如LANGUANE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"。
7. LANG
LC_*的默認值,是最低級別的設置,如果LC_*沒有設置,則使用該值。類似於 LC_ALL。
8. LC_ALL
它是一個宏,如果該值設置了,則該值會覆蓋所有LC_*的設置值。注意,LANG的值不受該宏影響。
"C"是系統默認的locale,"POSIX"是"C"的別名。所以當我們新安裝完一個系統時,默認的locale就是C或POSIX。
"POSIX" :Specifies the minimal environment for C-language translation called the POSIX locale. If setlocale() is not invoked, the POSIX locale is the default "C" Equivalent to "POSIX".-----------------------------------
How to view the current locale setting?
# locale
How to change the locale setting?
* Via the CDE login locale
* As a user-specific locale
* As a system default locale
To change the current locale setting, first confirm that the desired locale is
installed on the system with:
# locale -a
de
en_AU
en_CA
en_UK
C
If the desired locale is not in the list, you will need to install the
appropriate packages for that locale. See the Note below for more information
about locale packages.
* As a user-specific locale
* As a system default locale
To change the current locale setting, first confirm that the desired locale is
installed on the system with:
# locale -a
de
en_AU
en_CA
en_UK
C
If the desired locale is not in the list, you will need to install the
appropriate packages for that locale. See the Note below for more information
about locale packages.
How to change the locale via the CDE login locale?
On the CDE login banner:
Choose options - languages
Under languages - choose the new locale
The CDE banner will re-cycle and then you can login to the selected locale.
NOTE: If a user has a different default locale set in their environment, the
that locale setting will override the selected CDE login locale.
How to set a user-specific locale?
Note:
For sh, ksh:
# LANG=C; export LANG
# LC_ALL=C; export LC_ALL
For csh:
# setenv LANG C
# setenv LC_ALL C
Note: To set a default locale for a user's environment, set the LANG or LC_*
variables in a user's shell intialization file such as $HOME/.profile or
$HOME/.cshrc
For sh, ksh:
# LANG=C; export LANG
# LC_ALL=C; export LC_ALL
For csh:
# setenv LANG C
# setenv LC_ALL C
Note: To set a default locale for a user's environment, set the LANG or LC_*
variables in a user's shell intialization file such as $HOME/.profile or
$HOME/.cshrc
How to change the locale by setting the system default locale?
LANG=C LC_ALL=C Example from the /etc/default/init file: # Lines of this file should be of the form VAR=value, where VAR is one of # TZ, LANG, or any of the LC_* environment variables. LANG=C LC_ALL=C Note: The system must be rebooted after making changes to the /etc/default/init file in order for the changes to take effect. How to verify the locale setting? After setting or changing the locale, verify that the locale is set correctly: Check if the locale is set correctly by running the locale command without any options: # locale LANG=C LC_CTYPE= "C" LC_NUMERIC= "C" LC_TIME= "C" LC_COLLATE= "C" LC_MONETARY= "C" LC_MESSAGES= "C" LC_ALL=C
Locale是某一個地域內的人們的語言習慣和文化傳統和生活習慣,是根據計算機用戶所使用的語言,所在國家或者地區,以及當地的文化傳統所定義的一個軟件運行時的語言環境。
Locale是軟件在運行時的語言環境, 它包括語言(Language), 地域 (Territory) 和字符集(Codeset)。一個locale的書寫格式為: 語言[_地域[.字符集]]。完全的locale表達方式是 [語言[_地域][.字符集] [@修正值]。zh_CN.GB2312=中文_中華人民共和國+國標2312字符集。
locale的設定:
LC_ALL和LANG優先級的關系: LC_ALL > LC_* >LANG
1、如果需要一個純中文的系統的話,設定LC_ALL= zh_CN.XXXX,或者LANG=zh_CN.XXXX都可以。
2、如果只想要一個可以輸入中文的環境,而保持菜單、標題,系統信息等等為英文界面,那么只需要設定 LC_CTYPE=zh_CN.XXXX,LANG=en_US.XXXX就可以了。
3、假如什么也不做的話,也就是LC_ALL,LANG和LC_*均不指定特定值的話,系統將采用POSIX作為lcoale,也就是C locale。
LANG和LANGUAGE的區別:
LANG - Specifies the default locale for all unset locale variables
LANGUAGE - Most programs use this for the language of its interface
LANGUAGE是設置應用程序的界面語言。而LANG是優先級很低的一個變量,它指定所有與locale有關的變量的默認值