Strovent SystemsSign in
Reference

Accented or Unicode characters show as ? or boxes over SSH: LANG, LC_ALL and UTF-8

Why é, ü or emoji show as ?, as é or as boxes in an SSH session, how to see which side is wrong with locale, and how to set a UTF-8 locale or convert a file.

Over SSH, file names or text with accents, umlauts or emoji come out wrong. What you see tells you where the problem is.

| You see | Usually means | |---|---| | ? in place of each letter, for example caf? or ?? | the host program thinks the text is not printable: its locale is not UTF-8 | | two odd characters per letter, such as é for é | UTF-8 text shown as Latin-1, or the file really is double-encoded | | a box or a blank | the text arrived, but the font has no picture for that character |

Check the locale on the host

Programs decide how to handle text from the locale, set by environment variables. Run on the host:

locale
locale -c charmap

If locale -c charmap says UTF-8, the locale is fine. If it says ANSI_X3.4-1968 (plain ASCII), the session is in the C or POSIX locale. Programs then treat every non-ASCII byte as not printable. GNU ls, for example, prints a question mark for each such byte when its output is a terminal.

The variables are read in this order: LC_ALL wins over everything; then the per-category variable, such as LC_CTYPE; then LANG.

Fix the locale

Set a UTF-8 locale that exists on the host (locale -a lists them; replace C.UTF-8 below with one of those names):

export LANG=C.UTF-8
unset LC_ALL

Put the line in ~/.bashrc or ~/.profile to keep it. If your SSH client sends its own LANG or LC_* and the host does not have that locale, you get a warning at login instead. See Locale warning after SSH login, "setlocale: LC_ALL: cannot change locale".

Check the terminal

If the locale is UTF-8 and you still see é, the terminal is decoding as something other than UTF-8, or the text itself is wrong. Set the terminal's character set to UTF-8. Mosh needs UTF-8 on both ends anyway: see Mosh "needs a UTF-8 native locale to run".

The file itself is in another encoding

Old files are often Latin-1 or Windows-1252. Convert a copy with iconv:

iconv -f ISO-8859-1 -t UTF-8 old.txt > new.txt

Boxes

A box means the character arrived correctly but the font cannot draw it. Choose a font with wider coverage. Emoji and symbols also take up two columns in some programs and one in others, which can shift the rest of the line.

Sources

Spotted a mistake? Email [email protected].