Locale, CRLF and filenames
Locale affects character classification, collation, messages and multibyte processing. LC_ALL overrides category-specific LC_* variables and LANG; LANG is the fallback. LC_ALL=C sort gives a predictable byte-oriented ordering for a fixed input. A UTF-8 locale can count a multibyte character as one character while wc -c counts its bytes; do not assume locale names such as C.UTF-8 exist everywhere. Use locale -a where available. Preserve UTF-8 text where appropriate rather than forcing C for all application behavior.
Unix text lines normally end in LF (hex 0a). Windows-style CRLF adds CR (0d); reading a line may leave CR in the value, and a CR in a shebang can cause the interpreter path to be wrong on Linux. Inspect bytes before converting a known text file; blindly removing all CR bytes can damage meaningful content. Preserve LF in shell source and use version-control attributes when appropriate. Filenames may contain spaces, tabs and newlines; a NUL byte cannot be part of a Unix pathname, and slash separates path components. Prefer quoted operands, direct glob iteration or find -exec; avoid line-delimited filenames. Case sensitivity and Unicode normalization vary by filesystem and mount, even on one OS. Windows path syntax and WSL-mounted paths need separate testing. Do not normalize or rename arbitrary paths without understanding the host. The CRLF example removes one trailing carriage return from a known complete text record, not all carriage returns in the file. The filename example uses a fresh directory, so an existing directory is a setup failure.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Stable ASCII sort
Expected stdout:
AZazC locale sorts these ASCII bytes in increasing order.
Inspect CRLF and remove a record suffix
printf 'ok\r\n' > record.txt || exit 1od -An -tx1 record.txt || exit 1IFS= read -r line < record.txt || exit 1line=${line%$'\r'}printf '[%s]\n' "$line"Expected stdout:
6f 6b 0d 0a[ok]The hex view exposes CR. The Bash-specific suffix removal handles the known CRLF format, not arbitrary binary data.
Preserve newline-containing names
mkdir data || exit 1name=$'two\nlines.txt'printf 'x' > "data/$name" || exit 1count=0for file in data/*; do [ -f "$file" ] || continue count=$((count+1))doneprintf 'files=%s\n' "$count"Expected stdout:
files=1Glob expansion provides the filename as one word; quoted tests preserve it.
Common mistakes
Do not interpret wc -c as a character count or remove every CR from arbitrary data. Do not assume case-sensitive names are distinct on every filesystem.
Practice
Create a synthetic CRLF text file, inspect its bytes, strip only a trailing CR for a known record format and verify the result. Compare byte and character counts in installed locales.
Next: Cross-shell compatibility testing