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.
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
}