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.
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.
printf 'b\nA\na\nB\n' | LC_ALL=C sortWith 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.
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.