First, preface
Xshell compared to GNOME terminal, both are terminal emulators (in the Xshell can also execute simple built-in commands such as "CD", "LS", etc.), the same status.
Second, the principle analysis
Involved in garbled, then need to understand the encoding and decoding process. Execute the command in the terminal emulator, as shown in the communication procedure 1.
Figure 1
In the above communication process, a series of encoding and decoding processes occurred at the "Command execution unit", and a series of encoding and decoding processes occurred at "terminal Simulator", in addition, we often create an SSH connection to establish a remote shell session, in which the communication process 2, as shown in Figure 2, In this scenario, compared to the original communication process, only the "Command execution unit" and "terminal emulator" between the communication data through the SSH secure channel only, generally does not affect the overall encoding and decoding process.
Figure 2
A series of encoding and decoding procedures occur during command execution unit execution, and different commands have different implementations, so the encoding and decoding process that occurs during different command execution is not the same (the following links can be used as evidence for this conclusion: "Using VI and less to view text appears in Chinese garbled, Use cat Normal, "View log file more normal, use less to see garbled"). In addition, many of the commands will read the configuration values of locale variables (such as "LANG", "LANGUAGE", "Lc_all", "lc_ctype", etc.) for the encoding and decoding process during execution.
For example, "uniq" command to use the "lc_collate" environment variables such as configuration values, "grep" command to use "Lc_all" environment variables such as configuration values, "vim" command to use "LC_CTYPE" environment variables such as the configuration values.
A series of encoding and decoding processes also occur during the execution of commands at the terminal emulator, with 3 main elements:
1. Configure the encoding scheme used by the terminal simulator, for Gnome Terminal, in the location shown in Figure 3, for Xshell, in the position shown in Figure 4, for xftp, in the position shown in Figure 5 is configured
2, the execution of the command, using the Terminal emulator Configuration encoding scheme, the original command string encoding, the resulting byte stream to the "Command execution unit"
3, show command execution results, get the command execution result in the form of byte stream, use the encoding scheme configured by Terminal Simulator, decode it, get command execution result in string form.
Figure 3
Figure 4
Figure 5
third, the experiment
3.1. ls command
3.1.1, experiment 1
A file called "Hello. txt" is available, and the stored content corresponding to the file name is stored with a byte stream encoded using the "UTF-8" encoding scheme.
Set different locale series environment variable configuration values and terminal emulator encoding scheme, get different "ls command" execution results, as shown in table 1.
Table 1
Locale Series environment variable configuration values |
Terminal Simulator (experiment with GNOME terminal and Xshell) coding scheme |
ls command execution result |
Zh_cn.utf8 |
UTF-8 |
|
Zh_cn.gbk |
UTF-8 |
|
Zh_cn.utf8 |
GBK |
|
Zh_cn.gbk |
GBK |
|
3.1.2, experiment 2
A file called "Hello. txt" is available, and the stored content corresponding to the file name is stored with a byte stream encoded using the "GBK" encoding scheme.
Set different locale series environment variable configuration values and terminal emulator encoding scheme, get different "ls command" execution results, as shown in table 2.
Table 2
Locale Series environment variable configuration values |
Terminal Simulator (experiment with GNOME terminal and Xshell) coding scheme |
ls command execution result |
Zh_cn.utf8 |
UTF-8 |
|
Zh_cn.gbk |
UTF-8 |
|
Zh_cn.utf8 |
GBK |
|
Zh_cn.gbk |
GBK |
|
3.2. Vim command
3.2.1, experiment 1
There is a file called "A.txt" that stores the content "How are you?" ", the encoding format used is" UTF-8 ".
Set different locale series environment variable configuration values and terminal emulator encoding scheme to get different "vim a.txt" execution results, as shown in table 3.
Table 3
Locale Series environment variable configuration value (same as "3.1, ls command", also set the configuration values of all environment variables in the locale series to the same value) |
Terminal Simulator (experiment with GNOME terminal and Xshell) coding scheme |
"vim a.txt" command execution results |
": E ++enc=utf8" results in a vim environment |
Zh_cn.utf8 |
UTF-8 |
|
|
Zh_cn.gbk |
UTF-8 |
|
|
Zh_cn.utf8 |
GBK |
|
|
Zh_cn.gbk |
GBK |
|
|
3.2.2, Experiment 2
There is a file called "A.txt" that stores the content "How are you?" ", the encoding format used is" GBK ".
Set different locale series environment variable configuration values and terminal emulator encoding scheme to get different "vim a.txt" execution results, as shown in table 4.
Table 4
Locale Series environment variable configuration value (same as "3.1, ls command", also set the configuration values of all environment variables in the locale series to the same value) |
Terminal Simulator (experiment with GNOME terminal and Xshell) coding scheme |
"vim a.txt" command execution results |
": E ++enc=gbk" results in a vim environment |
Zh_cn.utf8 |
UTF-8 |
|
|
Zh_cn.gbk |
UTF-8 |
|
|
Zh_cn.utf8 |
GBK |
|
|
Zh_cn.gbk |
GBK |
|
|
Iv. Other
4.1. Locale Series Environment Variables
In the above experiment, the configuration values of all environment variables in the locale series are set to "ZH_CN.GBK" or "Zh_cn.utf8", in fact, "LS command implementation" or "VIM command implementation" only needs to read the configuration values of the environment variables of specific parts of the locale series. The configuration values of other environment variables in the locale family do not affect the operation of the "LS command" or "Vim command". It's just that we don't know what the "environment variables for specific parts of the locale series" are mentioned above, so for simplicity, configure values for all environment variables in the locale series to "ZH_CN.GBK" or "Zh_cn.utf8".
4.2. The main encoding and decoding process that the VIM command reads the contents of the file is inferred.
From the "3.2, Vim command" in the experiment, the use of the VIM command to read the file contents of the main encoding and decoding process occurs as follows: Read the file content of the byte stream, using the locale series environment variable specified encoding scheme (also can be ": E ++enc=?") The command itself specifies the encoding scheme to be used) to decode the byte stream to get the character stream of the file content, and then use the encoding scheme specified by the locale series environment variable to encode the obtained character stream to get the byte stream, and pass the byte stream of the preceding encoding to the terminal simulator.
Thus, as long as the locale series environment variable specified encoding scheme is consistent with the encoding scheme used by the terminal emulator, then directly can get the file content of the non-garbled display or through the ": E ++enc=?" command to get the file contents of the non-garbled display.
4.3. Two scripts used in the experiment
Script 1:
#!/bin/bash cd /home/dsl/tmp/shellexport LC_ALL=$1export LANG=$1export LANGUAGE=$1localeecho"---------"echo"---------"echo"---------"ls
Script 2:
#!/bin/bash cd /home/dsl/tmp/shellexport LC_ALL=$1export LANG=$1export LANGUAGE=$1localeecho"---------"echo"---------"echo"---------"vim a.txt
Error solving for executing commands in the Gnome Terminal,xshell terminal emulator.