操作系統字符集
# 查看操作系統支持的所有字符集
$ locale -a
# 查看操作系統支持的中文字符集
$ locale -a | grep zh
# 查看當前系統字符集
$ locale
或
$ echo $LANG
或
$ env |grep LANG
或
# Centos7 字符集配置文件,Centos6 爲: cat /etc/sysconfig/i18n
$ cat /etc/locale.conf
# 臨時設置字符集
$ LANG=zh_CN.UTF-8
# Centos7 設置字符集永久生效 ,Centos6 爲:echo "LANG=zh_CN.UTF-8" > /etc/sysconfig/i18n
$ echo "LANG=zh_CN.UTF-8" > /etc/locale.conf
文件字符集
# 查看文件字符集
$ file testString.sh
testString.sh: Bourne-Again shell script, UTF-8 Unicode text executable
或
$ vi testString.sh
:set fileencoding
# 查看文件內容,中文正常輸出
$ cat testString.sh |head -2
#!/bin/bash
# 字符串操作符實例
# 使用 iconv 轉換文件字符集,iconv -f 原編碼 -t 轉換後的編碼 inputfile -o outputfile
$ iconv -f utf-8 -t utf-16 testString.sh -o testString-utf16.sh
# 查看轉換後的字符集
$ file testString-utf16.sh
testString-utf16.sh: Bourne-Again shell script, Little-endian UTF-16 Unicode text executable
# 查看轉換爲 utf-16 字符集後,中文爲亂碼
$ cat testString-utf16.sh |head -2
��#!/bin/bash
# W[&{2N�d\O&{�[�O
微信公衆號:daodaotest