bash - ssh backspace deletes one character instead of two (utf-8 symbols)
2014-07
Im accessing a remote computer using ssh and running a bash command to input symbols using standard input stream:
read test;echo $test
It's ok to input english letters and use backspace to delete them, then input other characters and iterate this steps any amount of times. But it becomes a disaster when I use other utf-8 symbols, like russian ones, which require 2 bytes instead of 1, as english ones do. E.g., I input:
абвгдежз
After that, I use backspace for 8 times, and input other symbols:
привет
And press Enter key. But instead of the result which would equal my last input, I get the following:
абвгпривет
Which means that 8 backspace deleted only 4 symbols.
I do use linux ubuntu 12.04. Tried different consoles: standard one, terminator; different types of backspace options (as ASCII-del or Ctrl+H); setting $LC_TYPE to ru_RU.UTF-8, but nothing works. There is no such a problem using local console. Thank you in advance.
update: I've also got a video of this issue, please watch it at youtube
You can work around this by running
stty iutf8
on the server each time you ssh there. Unfortunately this bug exists since 2005 year, watch though the debian forums for details. You may also want to add this line at the end of your .bashrc file.
You need to set LC_CTYPE
to a UTF-8 locale, e.g. ru_RU.UTF-8
or en_US.UTF-8
. You can list available locales using locale -a
.
I have a fresh Ubuntu install and I want to connect over ssh in a gnome-terminal. The server uses latin1 (All files etc. are latin1), so I want to use that in the session. I have changed the charset in the menu-option so that characters are output correctly to my screen, but I can't input non-ascii correctly. Should I pass some magic arguments to ssh or is there a setting in gnome-terminal, or should I use stty? I'm a bit lost.
Update:
OK. I have now narrowed the problem down a bit. If I run the following on the command line:
php -r 'while ($c = fread(STDIN, 1)) { echo $c; }'
And press a non-ascii key, it echoes out correctly. However, if I type the same key in the shell, nothing happens. So this must be some setting in the shell environment (Locale setting?). Any ideas?
The problem most likely has to do with readline, which bash uses. Put the following in either /etc/inputrc
or ~/.inputrc
:
set meta-flag on
set output-meta on
set convert-meta off
meta-flag
enables eight-bit input (that is, it will not clear the eighth bit in the characters it reads), regardless of what the terminal claims it can support. output-meta
will enable the display of characters with the eighth bit set directly rather than as a meta-prefixed escape sequence. When convert-meta
is on, readline converts characters with the eighth bit set to an ASCII key sequence by stripping the eighth bit and prefixing it with an escape character (in effect, using escape as the meta prefix). We turn it off. Do man readline
for more information about these and other variables.
You can change the encoding under Terminal->Encoding on the menu in gnome-terminal. Add the Western (ISO 8859-1) encoding, then switch to it.