Linux Command Reference ยท System Information
Linux locale command
Reference for the Linux locale command: show LANG and LC_* variables, list installed locales, understand LC_ALL precedence and fix sorting and encoding problems.
A locale is a bundle of cultural rules that programs apply to text: which character encoding to use, how to sort words, how to format dates, numbers and currency, and which language to show in messages. The locale command prints the active settings; locale -a lists the locales installed on the machine.
Settings come from environment variables: LANG is the default, the LC_* variables override single categories, and LC_ALL overrides everything. The locale affects scripts more than people expect, because it changes how sort, grep ranges, tr and date behave.
locale
locale -a
LC_ALL=C sort file
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
locale [-a | -m | -c -k NAME | NAME...]
| Option / variable | Description |
|---|---|
locale |
Print the current value of LANG, every LC_* category and LC_ALL. |
-a, --all-locales |
List installed locales. |
-m, --charmaps |
List available character maps (encodings). |
locale NAME |
Print the value of one keyword, e.g. locale charmap or locale decimal_point. |
-k, --keyword-name |
Print the keyword names together with values. |
LANG |
Default for every category that is not set. |
LC_CTYPE |
Character classes, case conversion and encoding. |
LC_COLLATE |
Sort order. |
LC_TIME, LC_NUMERIC, LC_MONETARY |
Dates, numbers, currency. |
LC_MESSAGES |
Language of program messages and prompts. |
LC_ALL |
Overrides all of the above, including LANG. |
LANGUAGE |
GNU gettext message language priority list; only used if the locale is not C. |
Returns: 0 on success. If a variable names a locale that is not installed, locale prints warnings to standard error and the program falls back to C.
Usage notes
- Precedence:
LC_ALLbeatsLC_xxxbeatsLANG. A singleLC_ALL=Cin a command is the standard way to get reproducible behavior. - Naming:
language_TERRITORY.encoding, for examplede_DE.UTF-8.C(alsoPOSIX) is the minimal locale: ASCII, byte-wise sorting, English messages.C.UTF-8keeps byte order but understands UTF-8. - Sorting surprises: in
en_US.UTF-8,sortignores case and punctuation to some degree; inCall uppercase letters come before lowercase. UseLC_ALL=C sortfor stable output in scripts and for speed. - Why
[a-z]can match capitals in some locales: ranges follow the collation order. Use POSIX classes like[[:lower:]]for portable code. - Performance: many tools are much faster in the
Clocale because they avoid multibyte logic (LC_ALL=C grepon large files). - Missing locale: "cannot change locale" or
perl: warning: Setting locale failedmeans the locale is requested but not generated. On Debian/Ubuntu usesudo dpkg-reconfigure localesorlocale-gen; on RHEL/Fedora installglibc-langpack-*. - Containers often have only
C,C.UTF-8andPOSIX. SetLANG=C.UTF-8there to get UTF-8 handling without installing anything. - SSH forwards
LANGandLC_*by default, which can trigger the warning above on the remote host.
Related
sort,date,iconv,env- Lessons: Environment, Sorting, uniq and cut