Americas

  • United States
sandra_henrystocker
Unix Dweeb

Using the Linux locale command

How-To
Jan 03, 20238 mins
Linux

The locale settings on Linux systems can sync dates and times with where you are in the world.

The locale settings in Linux systems help ensure that information like dates and times are displayed in a format that makes sense in the context of where you live and what language you speak. Here’s how to use them.

NOTE: None of the commands described in this post will change your locale settings. Some merely use a different locale setting to display the response you might be seeing from a different location.

List your settings

If you’re in the US, you should see something like this when you use the locale command to list your settings:

$ locale
LANG=en_US.UTF-8
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=

The en_US.UTF-8 settings in the above output all represent US English. If you’re in France, this response is more likely:

LANG=fr_FR.utf-8
LC_CTYPE="fr_FR.utf8"
LC_NUMERIC="fr_FR.utf8"
LC_TIME="fr_FR.utf8"
LC_COLLATE="fr_FR.utf8"
LC_MONETARY="fr_FR.utf8"
LC_MESSAGES="fr_FR.utf8"
LC_PAPER="fr_FR.utf8"
LC_NAME="fr_FR.utf8"
LC_ADDRESS="fr_FR.utf8"
LC_TELEPHONE="fr_FR.utf8"
LC_MEASUREMENT="fr_FR.utf8"
LC_IDENTIFICATION="fr_FR.utf8"
LC_ALL=fr_FR.utf8

Hundreds of lines of output will be displayed if you use the locale -a (list all locales) command since it lists locales from around the globe.

$ locale -a | wc -l
869

Here’s a sample:

$ locale -a | column | head -10
aa_DJ                   es_CO.iso88591          ms_MY.utf8
aa_DJ.iso88591          es_CO.utf8              mt_MT
aa_DJ.utf8              es_CR                   mt_MT.iso88593
aa_ER                   es_CR.iso88591          mt_MT.utf8
aa_ER@saaho             es_CR.utf8              my_MM
aa_ER.utf8              es_CU                   my_MM.utf8
aa_ER.utf8@saaho        es_CU.utf8              nan_TW
aa_ET                   es_DO                   nan_TW@latin
aa_ET.utf8              es_DO.iso88591          nan_TW.utf8
af_ZA                   es_DO.utf8              nan_TW.utf8@latin

Like the en_US settings shown above, these codes, stand for the language and county (e.g., aa_DJ stands for Afar, Djibouti). You can display a complete list with a command like the following that selects the portions on lines prior to the periods and limits the output to one of each before organizing the listing in columns:

$ locale -a | grep "." | awk -F. '{print $1}' | uniq | column
aa_DJ           ca_FR           es_DO           is_IS           nhn_MX          st_ZA
aa_ER           ca_IT           es_EC           it_CH           niu_NU          sv_FI
aa_ET           ce_RU           es_ES           it_IT           niu_NZ          sv_SE
af_ZA           chr_US          es_GT           iu_CA           nl_AW           sw_KE
agr_PE          ckb_IQ          es_HN           ja_JP           nl_BE           sw_TZ
ak_GH           cmn_TW          es_MX           japanese        nl_NL           szl_PL
am_ET           crh_UA          es_NI           kab_DZ          nn_NO           ta_IN
an_ES           csb_PL          es_PA           ka_GE           no_NO           ta_LK
anp_IN          cs_CZ           es_PE           kk_KZ           nr_ZA           tcy_IN
ar_AE           C               es_PR           kl_GL           nso_ZA          te_IN
ar_BH           cv_RU           es_PY           km_KH           oc_FR           tg_TJ
ar_DZ           cy_GB           es_SV           kn_IN           om_ET           the_NP
ar_EG           da_DK           es_US           kok_IN          om_KE           th_TH
ar_IN           de_AT           es_UY           ko_KR           or_IN           ti_ER
ar_IQ           de_BE           es_VE           korean          os_RU           ti_ET
ar_JO           de_CH           et_EE           ks_IN           pa_IN           tig_ER
ar_KW           de_DE           eu_ES           ku_TR           pap_AW          tk_TM
ar_LB           de_IT           fa_IR           kw_GB           pap_CW          tl_PH
ar_LY           de_LI           ff_SN           ky_KG           pa_PK           tn_ZA
ar_MA           de_LU           fi_FI           lb_LU           pl_PL           to_TO
ar_OM           doi_IN          fil_PH          lg_UG           ps_AF           tpi_PG
ar_QA           dsb_DE          fo_FO           li_BE           pt_BR           tr_CY
ar_SA           dv_MV           fr_BE           lij_IT          pt_PT           tr_TR
ar_SD           dz_BT           fr_CA           li_NL           quz_PE          ts_ZA
ar_SS           el_CY           fr_CH           ln_CD           raj_IN          tt_RU
ar_SY           el_GR           fr_FR           lo_LA           ro_RO           ug_CN
ar_TN           en_AG           fr_LU           lt_LT           ru_RU           uk_UA
ar_YE           en_AU           fur_IT          lv_LV           ru_UA           unm_US
as_IN           en_BW           fy_DE           lzh_TW          rw_RW           ur_IN
ast_ES          en_CA           fy_NL           mag_IN          sah_RU          ur_PK
ayc_PE          en_DK           ga_IE           mai_IN          sa_IN           uz_UZ
az_AZ           en_GB           gd_GB           mai_NP          sat_IN          ve_ZA
az_IR           en_HK           gez_ER          mfe_MU          sc_IT           vi_VN
be_BY           en_IE           gez_ET          mg_MG           sd_IN           wa_BE
bem_ZM          en_IL           gl_ES           mhr_RU          se_NO           wae_CH
ber_DZ          en_IN           gu_IN           mi_NZ           sgs_LT          wal_ET
ber_MA          en_NG           gv_GB           miq_NI          shn_MM          wo_SN
bg_BG           en_NZ           hak_TW          mjw_IN          shs_CA          xh_ZA
bhb_IN          en_PH           ha_NG           mk_MK           sid_ET          yi_US
bho_IN          en_SC           he_IL           ml_IN           si_LK           yo_NG
bho_NP          en_SG           hif_FJ          mni_IN          sk_SK           yue_HK
bi_VU           en_US           hi_IN           mn_MN           sl_SI           yuw_PG
bn_BD           en_ZA           hne_IN          mnw_MM          sm_WS           zh_CN
bn_IN           en_ZM           hr_HR           mr_IN           so_DJ           zh_HK
bo_CN           en_ZW           hsb_DE          ms_MY           so_ET           zh_SG
bo_IN           eo              ht_HT           mt_MT           so_KE           zh_TW
br_FR           es_AR           hu_HU           my_MM           so_SO           zu_ZA
brx_IN          es_BO           hy_AM           nan_TW          sq_AL
bs_BA           es_CL           ia_FR           nb_NO           sq_MK
byn_ER          es_CO           id_ID           nds_DE          sr_ME
ca_AD           es_CR           ig_NG           nds_NL          sr_RS
ca_ES           es_CU           ik_CA           ne_NP           ss_ZA

Notice that only four of the codes don’t use two-part naming: “C” for the POSIX standards-compliant format, “eo” for “esperanto” (the world’s most widely spoken constructed international auxiliary language), Japanese and Korean .

Viewing  and looping through months and days

I discovered recently that I the locale command can display the months of the year or the days of the week with commands like these:

$ locale mon
January;February;March;April;May;June;July;August;September;October;November;December
$ locale day
Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday

The above commands make looping through the months of the year or the days of the week surprisingly easy. For example, to loop through the months, use a command like this:

$ months=`locale mon | sed 's/;/ /g'`  
$ for mo in $months
> do
>   echo $mo
> done
January
February
March
April
May
June
July
August
September
October
November
December

To loop through the days of the week, try this:

$ weekdays=`locale day | sed 's/;/ /g'`
$ for day in $weekdays
> do
>   echo $day
> done
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Change the lauguage used for months and days

You can also use a locale setting to display the months of the year or the days of the week in some other language. For example, to display the months in French, use a command like this one:

$ LC_ALL=fr_FR.utf8 locale mon
janvier;février;mars;avril;mai;juin;juillet;août;septembre;octobre;novembre;décembre

To see the months in Swahili instead, do this:

$ LC_ALL=sw_TZ.utf8 locale mon
Januari;Februari;Machi;Aprili;Mei;Juni;Julai;Agosti;Septemba;Oktoba;Novemba;Desemba

To list the current date and time in Russian and Italian, use commands like these:

$ LC_ALL=os_RU.utf8 date
Крс 02 Янв 2023 11:35:31 EST
$ LC_ALL=it_IT.utf8 date
dom 3 gen 2023, 16:29:10, EST

Wrap-Up

Your locale settings are in place to ensure that date, time, and related details are displayed in the format that makes sense to you. They can, as described in this post, also allow you to generate month-of-year and day-of-week loops and to display date/time information in other languages.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author