$_ CLI In Depth
Learning progress 0%
lesson environment path dotfiles

Locale, LANG & LC_ALL

5 min read Module 9: Environment, PATH & Dotfiles Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:20

What a locale controls

A program that prints text, sorts lines, or formats a number must decide what the rules are. The C library does this by asking the locale. The locale covers character classes, such as whether a byte counts as an uppercase letter, the collating order used by sort, the decimal separator used by formatted numbers, the date format, and the language of many messages. A program that does not set its locale behaves like the C locale, which is the minimal set of rules.

The categories and their variables

The locale is split into categories. The most important ones for command-line work are LC_CTYPE, which decides how bytes are read as characters, and LC_COLLATE, which decides how sort compares strings. Others include LC_NUMERIC for decimal separators, LC_TIME for date names, and LC_MESSAGES for the language of error messages. Each category has its own variable, and LANG is the default for all of them.

The priority rule

Bash and the C library look at the variables in a fixed order. LC_ALL wins over everything. If it is empty or unset, the category variable such as LC_COLLATE wins. If that is also unset, LANG is used. If none is set, the C locale applies. This rule is why a stray LC_ALL in one file can override a carefully chosen LANG in another.

Note

The value C is a special name that means the minimal locale. It sorts by byte value, so uppercase letters come before lowercase letters. The name POSIX is an alias for C in most systems.

Why sort changes with the locale

Sorting is the clearest place to see a locale at work. A byte-value sort puts every uppercase letter before every lowercase letter. A locale with language-aware collation interleaves them, so a and A sit next to each other. Neither result is wrong, but a script that depends on a particular order must set the locale it needs.

BASH1 lines
printf 'b\nA\na\nB\n' | LC_ALL=C sort

With LC_ALL=C, the output is A, B, a, b, which is byte order. Under many UTF-8 locales the same input produces a different sequence. Setting LC_ALL=C for one command is the simplest way to make a script produce the same result on every machine.

Locales that may be missing

A locale only works if its data is installed. If you set LANG to a locale that is not present, programs usually fall back to C and may print a warning. Minimal systems, including the containers that many people use, often ship only the C locale and a UTF-8 variant of C. Check what is available with the locale -a command on systems where that tool is installed. When you write a script that must run on many machines, set LC_ALL to C for the parts that depend on order, and do not assume that a language locale exists.

Reading the variables in a clean environment

To see how the variables combine, start a program with a known set of values. The env -i command clears the environment, and then you name exactly the variables you want. This technique avoids the noise of your own settings.

BASH1 lines
env -i LANG=C.UTF-8 LC_ALL=C bash -c 'printf "%s\n" "LC_ALL=$LC_ALL" "LANG=$LANG"'

The output shows both variables exactly as set. Both are present in the environment, but only LC_ALL decides the character and collation rules for programs that read the locale, because it is the higher priority. Keep this order in mind whenever a tool behaves differently from what LANG suggests.

Which categories a program reads

The rule works the same way for every category. Set the variable that matches the question you are asking, leave the others alone, and check the result with a command whose output you already understand. A sort is a good first test, because the difference between byte order and a language order is easy to see in a short list of mixed-case words.

Not every program reads every category. Sort reads the collation and character rules, while printf reads the numeric category for the decimal separator. Many tools also read the messages category, so a message may be translated while sorting still uses byte order. Because each program chooses its categories, the same environment can produce different results for different tools. When an output surprises you, the first question is which category that tool reads.

Setting LC_ALL for one command is the most direct way to control every category for that run. When you only need to fix the order of a sort, setting LC_COLLATE is narrower and leaves the rest of the locale alone.

Character sets and the UTF-8 question

A locale name often ends in a character set, such as UTF-8. That suffix tells programs how bytes map to characters. A UTF-8 locale lets a program count a multibyte character as one character, while the C locale counts each byte separately. This matters for commands that cut or count by character, for listings that align names, and for any script that trims text by length. If a script must handle non-ASCII input, choose a UTF-8 locale that exists on every target machine, and test it on a sample string that contains a non-ASCII letter.

A practical habit

Set the locale explicitly in any script that sorts, compares, or parses text. Use LC_ALL=C when you need byte order and predictable output, and leave the user locale in place for messages that people read. Doing this once avoids a class of bugs that appear only on a colleague's machine.

Terminal lab · 2 steps · real Linux in your browser

Locale, LANG and LC_ALL

Force byte-order sorting with LC_ALL=C, then build a clean environment that sets the locale variables explicitly.

01
Sort in byte order
Create sort-demo.sh in /workspace. It writes four words with mixed case to words.txt, then sorts them with LC_ALL=C into sorted-c.txt: ``bash #!/bin/bash printf 'b\nA\na\nB\n' > /workspace/words.txt LC_ALL=C sort /workspace/words.txt > /workspace/sorted-c.txt ` Run bash sort-demo.sh and read sorted-c.txt. The order is A, B, a, b`.
Show hint for step 1
The C locale sorts by byte value, so every uppercase letter comes before every lowercase letter.
02
Set locale variables in a clean environment
Create locale-env.sh in /workspace. It starts a bash with a cleared environment, sets LANG and LC_ALL for that bash only, and saves the two values to locale-env.txt: ``bash #!/bin/bash env -i LANG=C.UTF-8 LC_ALL=C bash -c 'printf "%s\n" "LC_ALL=$LC_ALL" "LANG=$LANG"' > /workspace/locale-env.txt ` Run bash locale-env.sh and read locale-env.txt. It should contain LC_ALL=C and then LANG=C.UTF-8`.
Show hint for step 2
env -i gives the child an empty environment, so only the variables you name are present.
Check yourself Question 1 of 4
Concept Check Top of the priority order recall

Which variable overrides every LC_* category variable and LANG?

Recap & Key Takeaways

LC_ALL overrides the category variables, which override LANG.
LC_CTYPE controls character classes, and LC_COLLATE controls sort order.
LC_ALL=C gives byte-order sorting that is the same on every machine.
A locale that is not installed usually falls back to C, so check what exists before relying on it.

Primary Sources & Further Reading

Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue