25

I'm setting up the CPAN module for perl on CentOs 5, and one of the questions is 'Does your terminal support UTF-8?' (paraphrased). How do I find out?

8 Answers 8

17

Really, the surefire way to test is to download a text file and cat it in the terminal and see if everything looks ok.

or, if you can, recompile the terminal enabling the unicode option (assuming it has one).

what does $TERM and $LANG look like?

5
  • $TERM is "xterm" $LANG is "en_US.UTF-8" (aha!) The text file displays nicely in terminal, but curiously, not in Firefox.
    – Whatsit
    May 28, 2009 at 16:16
  • yea, i actually encountered the same thing :-/ ... when i tried therek's suggestion, i got the question mark May 28, 2009 at 17:02
  • $LANG just tells you what your system will use when writing to stdout/stderr. It doesn't say anything about the capabilities of the terminal. However, if everything your system prints of weird characters looks ok, then your terminal probably supports UTF-8.
    – Epcylon
    May 28, 2009 at 17:58
  • 3
    This can even be automated, by displaying some text and checking its width (by reading the cursor position before and after). I posted a proof of concept a propos something else. Apr 2, 2013 at 18:17
  • @Gilles, your answer is brilliantly simple. Love it!
    – user262363
    Jan 23, 2018 at 19:50
27

Type this in your terminal:

echo -e '\xe2\x82\xac' 

If your terminal supports UTF-8 it will output the euro sign:

1
  • Not all fonts have the euro sign, so a different test code point might be advisable. Apr 2, 2013 at 18:17
5

The lamest way: run following and check the output. It will be a capital O with circumflex if the terminal displays UTF-8.

perl -le 'print "\x{c3}\x{94}"'
4
  • I'm pretty sure my terminal supports UTF-8 now (passed the test suggested by theman_on_osx) but this just outputs a blank line. What's going on?
    – Whatsit
    May 28, 2009 at 16:19
  • Maybe the font you're using in your terminal app does not support UTF-8 characters.
    – therek
    May 28, 2009 at 18:13
  • 4
    I know this is extremely late but the UTF-8 Out flag makes that work better perl -CO -le 'print "\x{d4}"'
    – Ashley
    Feb 8, 2011 at 21:49
  • 3
    Or without the -CO option, give Perl the correct UTF-8 bytes : $ perl -le 'print "\x{c3}\x{94}"' Ô
    – Tim
    Dec 31, 2012 at 20:49
2

The most sure fire way is to use the ‘locale’ command. It will print out all the various and sundry variables that dictate what character set to use. For instance, this is my output on RHEL5.3, set to only use UTF-8 by default.

LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=
1
  • 1
    This doesn't test the terminal, only the locale setting (which in practice isn't always set by the terminal emulator, may not reflect the current state of the terminal, or may have been overridden by some user configuration). Apr 2, 2013 at 18:12
1

You may just use the following command:

locale charmap
1
  • 1
    This doesn't test the terminal. It only displays the locale settings. Feb 11, 2013 at 15:16
1
curl http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt

or

wget -O - http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt

This obviously requires wget or curl.

0
UTF=$(echo -e "\u263A")
if [[ ! "$UTF" =~ "A" ]]  ; then
 echo -n "UNICODE here!"
fi
1
  • 2
    This doesn't test the terminal. It only tests whether the echo builtin supports \u. Feb 11, 2013 at 15:16
0

You can test the terminal by setting the cursor position to column 1 and outputing a multibyte unicode character. If the cursor moves by more than 1 position then the terminal does not support unicode.

On in this case we emit a 3 byte sequence which is a zero width space, so if the cursor moves at all, the terminal cannot process unicode

IFS=$';\x1B[' read -p $'\r\xE2\x80\x8B\x1B[6n\r   \r' -d R -rst 1 _ _ _ X _ </dev/tty 2>/dev/tty && test "$X" = 1

Here we output \r to get to position 1 and then emit a 3 byte sequence which is a zero width space, and then emit ESC [ 6n which asks the cursor position, followed by \r \r to overwrite any junk that will have appeared if the terminal handled each byte as a separate character.

Then we read the cusor position with a 1 second timeout and check whether the X position is position 1 which it will be if the terminal can process unicode.

A better function is:

is-tty-unicode() {
  local X

  test -c /dev/tty &&
  if test -t 0
  then IFS=$';\x1B[' read -p $'\r\xE2\x80\x8B\x1B[6n\r   \r' -d R -rst 1 _ _ _ X _ 2>&1
  fi <>/dev/tty && test "$X" = 1
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .