3

The locale program will print the locale variables of the process that launched it, this is a sample output of locale when launched from the shell:

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

Where does locale get this information from (I know that this information is not locale's inherited environment variables from the shell, because the shell only has 4 locale environment variables)?

Stephen Kitt
  • 434,908
James
  • 201

2 Answers2

3

It gets it by knowing how locale settings are processed, based on the values of the corresponding environment variables.

Taking the GNU version as an example, it starts by calling setlocale (LC_ALL, "") to set the current locale. Then it goes through all the locale categories, printing the value of each one in turn, with special exceptions for LANG (printed first) and LC_ALL (printed last). The values are determined by looking at the environment values and following the rules which apply to the locale settings:

  • if LC_ALL is set, all categories take the corresponding value;
  • if a category has no value set in the environment, it takes the value of LANG if it has one, “POSIX” otherwise and the value is enclosed in double-quotes.
Stephen Kitt
  • 434,908
  • So if a process have its current locale variables different from its locale variables in the environment variables, and the process launched the locale program, then the locale program will only output the locale variables in the environment variables, correct? – James Jun 12 '18 at 17:09
  • @James more or less. Locale settings aren’t inherited, yes, in particular because all processes run the equivalent of setlocale(LC_ALL, "C") when they start. The locale program will output the locales corresponding to whatever variables are present in the environment it was given (which is determined by the program which starts locale). – Stephen Kitt Jun 12 '18 at 17:15
0

The locale program prints it's results based on the environment variables.

The output you listed contains non-standard locales that may be Linux specific.

In order to understand how this works, it may be important to know:

  • LC_ALL overwrites all other explicit LC_* variables in the environment.

  • LANG is used as a fallback if a specific LC_XXX variable is not in the environment.

schily
  • 19,173