Locale Environment Variables in Linux


Locale Environment Variables in Linux

Locale environment variables in Linux play a crucial role in enabling users to communicate effectively with operating system. locale environment variables are responsible for setting language, encoding, and cultural conventions in a system. In this article, we will explore different aspects of locale environment variables in Linux.

What is a Locale?

A locale is a set of parameters that define user's language, country, currency, and other cultural conventions. locale defines format of dates, time, and numbers, and character sets. For example, US locale uses dollar as currency symbol, while UK locale uses pound. Similarly, US locale uses mm/dd/yyyy format for dates, while UK locale uses dd/mm/yyyy format.

A locale is identified by a name that comprises a language code, country code, and encoding. For example, US locale is identified by name en_US.UTF-8, where en stands for English, US stands for United States, and UTF-8 stands for Unicode Transformation Format.

Setting Locale in Linux

The locale environment variables can be set at different levels in Linux. highest level is system level, which applies to all users in system. system-level locale is defined in /etc/locale.conf file. file contains a single line that sets value of LANG variable, which is default locale for system. For example, to set US locale as default system locale, /etc/locale.conf file should contain following line −

LANG=en_US.UTF-8

The LANG variable sets language, country, and encoding for system. In addition to LANG variable, /etc/locale.conf file can also contain other variables that specify different aspects of locale, such as LC_TIME, LC_NUMERIC, LC_CURRENCY, LC_COLLATE, LC_MONETARY, and LC_MESSAGES.

The second level is user level, which applies to individual users in system. Each user has a home directory that contains a .bashrc file, which is a shell script that is executed when user logs in. .bashrc file can contain commands that set user's locale. For example, to set US locale for a user, .bashrc file should contain following line −

export LANG=en_US.UTF-8

The export command makes LANG variable available to all child processes of shell. In addition to LANG variable, .bashrc file can also contain other variables that specify different aspects of locale, such as LC_TIME, LC_NUMERIC, LC_CURRENCY, LC_COLLATE, LC_MONETARY, and LC_MESSAGES.

The third level is application level, which applies to individual applications in system. Each application can set its own locale environment variables. For example, Firefox web browser can set LANG variable to user's preferred locale. locale environment variables set by an application take precedence over user and system-level variables.

Managing Locale Environment Variables

The locale environment variables can be managed using locale command in Linux. locale command can be used to view current locale settings, as well as to set locale for current session or permanently.

To view current locale settings, run following command −

$ locale

The output will display values of different locale environment variables, such as LANG, LC_TIME, LC_NUMERIC, LC_CURRENCY, LC_COLLATE, LC_MONETARY, and LC_MESSAGES.

To set locale for current session, run following command −

$ export LANG=en_US.UTF-8

This command sets LANG variable to US locale for current session. new locale settings will be effective only for current session and will be lost when session is closed.

To permanently set locale, /etc/locale.conf file can be edited to set value of LANG variable. This change will apply to all users in system.

For example, to set US locale as default system locale, /etc/locale.conf file should contain following line −

LANG=en_US.UTF-8

In addition to LANG variable, other variables can also be set in file to specify different aspects of locale.

Examples of Using Locale Environment Variables

Let's explore some examples of how locale environment variables can be used in Linux.

Example 1: Setting Date and Time Format

The LC_TIME variable is used to set date and time format in system. variable can be set to a value that corresponds to a locale that defines desired format.

For example, to set date and time format to US format, run following command −

$ export LC_TIME=en_US.UTF-8

This command sets LC_TIME variable to US locale, which uses mm/dd/yyyy format for dates and 12-hour clock format for time.

Example 2: Setting Currency Symbol

The LC_MONETARY variable is used to set currency symbol in system. variable can be set to a value that corresponds to a locale that defines desired currency symbol.

For example, to set currency symbol to euro symbol, run following command −

$ export LC_MONETARY=en_US.UTF-8

This command sets LC_MONETARY variable to US locale, which uses dollar symbol as currency symbol. However, if a program uses LC_MONETARY variable, it will display euro symbol instead of dollar symbol.

Example 3: Sorting and Collation

The LC_COLLATE variable is used to set sorting and collation rules in system. variable can be set to a value that corresponds to a locale that defines desired sorting and collation rules.

For example, to set sorting and collation rules to Spanish locale, run following command −

$ export LC_COLLATE=es_ES.UTF-8

This command sets LC_COLLATE variable to Spanish locale, which uses traditional Spanish alphabet for sorting and collation.

Conclusion

Locale environment variables play a critical role in enabling effective communication between users and operating system. variables define language, encoding, and cultural conventions that are used in system. Linux provides several levels at which locale environment variables can be set, allowing users to customize their locale settings to suit their preferences. By understanding how to manage locale environment variables, users can ensure that their systems are set up to meet their specific needs.

Updated on: 23-Mar-2023

416 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements