Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
locale
locale -a
LC_ALL=C sort file

Try it

Bash Show the active locale and installed ones

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.

Bash LC_ALL changes sorting and character handling

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

Terminal
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_ALL beats LC_xxx beats LANG. A single LC_ALL=C in a command is the standard way to get reproducible behavior.
  • Naming: language_TERRITORY.encoding, for example de_DE.UTF-8. C (also POSIX) is the minimal locale: ASCII, byte-wise sorting, English messages. C.UTF-8 keeps byte order but understands UTF-8.
  • Sorting surprises: in en_US.UTF-8, sort ignores case and punctuation to some degree; in C all uppercase letters come before lowercase. Use LC_ALL=C sort for 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 C locale because they avoid multibyte logic (LC_ALL=C grep on large files).
  • Missing locale: "cannot change locale" or perl: warning: Setting locale failed means the locale is requested but not generated. On Debian/Ubuntu use sudo dpkg-reconfigure locales or locale-gen; on RHEL/Fedora install glibc-langpack-*.
  • Containers often have only C, C.UTF-8 and POSIX. Set LANG=C.UTF-8 there to get UTF-8 handling without installing anything.
  • SSH forwards LANG and LC_* by default, which can trigger the warning above on the remote host.