Solution for Terminal Garbled Characters "^[[A^[[D" in Linux (Debian 11)

Publish: 2023-04-24 | Modify: 2023-04-24

Recently, Xiaoz encountered an issue when using the same SSH client on a VPS terminal. When pressing the up arrow ↑ and down arrow ↓, garbled symbols "^[[A^[[D" appeared. I tried using ChatGPT and searching on the internet but couldn't find a solution. Eventually, I relied on my experience to solve the problem.

image

Temporary Solution

Xiaoz is using Debian 11 as the operating system, and this issue occurs for non-root users (should be the same for other Linux distributions). Observant friends may notice that there is a "$" symbol at the beginning of the terminal, indicating that the default shell is "sh". To verify the shell type, enter the command "echo $SHELL":

$ echo $SHELL
/bin/sh

As expected, the shell is "sh". Then I entered the "bash" command to switch the shell to "bash" mode, and the symbol at the beginning changed to "user@GreenCloud:/data$".

image

When I pressed the up arrow ↑ and down arrow ↓ again to view the command history, the garbled symbols no longer appeared. The problem was solved.

Permanent Solution

The above method is only a temporary fix, and the issue will still exist next time you log in. We can use the "chsh" command to change the default shell type for a specified user. The command is as follows:

# /bin/bash is the shell type, replace "user" with your own username
chsh -s /bin/bash user

The next time the user logs in, they will use "bash" as their default shell terminal type. This way, the problem is permanently resolved.

Conclusion

In Linux, if the user's shell type is "sh", it may cause garbled symbols "^[[A^[[D" when using the up arrow ↑ and down arrow ↓. This can be temporarily solved by using the "bash" command or permanently solved by using the "chsh" command to change the user's shell type. Users with similar situations can refer to this solution.


Comments