简体   繁体   中英

How to detect Unix terminal character encoding?

Is there a way to detect the character encoding set in the terminal which is calling my Java program? In Windows I can call the "chcp" tool and parse the output.

But what about in Linux or Mac?

Check the locale(1) man page

You can change this by setting the LANG environment variable

$ export LANG=en_US.iso88591

$ locale
LANG=en_US.iso88591
LC_CTYPE="en_US.iso88591"
LC_NUMERIC="en_US.iso88591"
LC_TIME="en_US.iso88591"
LC_COLLATE="en_US.iso88591"
LC_MONETARY="en_US.iso88591"
LC_MESSAGES="en_US.iso88591"
LC_PAPER="en_US.iso88591"
LC_NAME="en_US.iso88591"
LC_ADDRESS="en_US.iso88591"
LC_TELEPHONE="en_US.iso88591"
LC_MEASUREMENT="en_US.iso88591"

LC_IDENTIFICATION="en_US.iso88591"

There is no defined communication path from the terminal app to processes running inside a terminal window (short of those defined by the terminal being emulated, such as window size changes). This would be true on any other Unix-like OS as well. Environment variables can only be inherited when a process starts. Looking at the values of these environment variables is all you can do.

Do you really expect people to be changing the encoding while a terminal window is open? I can't imagine this is a common use case at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM