Format Numbers in Bash: Separators, Locales and When to Keep Plain Digits
How Bash formats numbers with thousands and decimal separators using printf, numfmt and locale settings, what the output changes, and when plain digits are safer.
Formatting numbers in a Bash script usually means adding grouping (thousands) separators, fixing the number of decimal places, or switching between a period and a comma as the decimal mark. The tools are small and built in or close to it: the printf builtin, the locale settings that steer it, and GNU coreutils' numfmt. This review explains what each approach changes, where it helps readers, and where it quietly breaks the next step in a pipeline.
Short verdict
Locale-aware formatting in Bash is suitable for output meant for people: terminal reports, log summaries, messages and printed tables. Its main benefit is that large values become easier to read at a glance, and the separators can match the reader's region. The decisive tradeoff is that the result is a new text string, not a number: other programs, Bash arithmetic, CSV files and searches may no longer recognize it. Keep plain, unformatted digits, typically with the C locale, whenever the output will be parsed, stored, compared, or passed to another tool.
What the transformation actually changes
This is a stored-character change, not a visual effect. When printf inserts a grouping separator, the output contains extra characters that were not in the input: 1234567 becomes a longer string such as 1,234,567. Nothing about display, fonts or rendering is involved; a program that reads the output receives the separators as ordinary characters. Bash itself has no formatted-number type, and its arithmetic expansion works only with integers, so any decimal places come from printf's floating-point conversions rather than from Bash arithmetic.
Which separators appear depends on the locale category LC_NUMERIC, which LC_ALL overrides if it is set. The apostrophe flag in a printf conversion such as %'d asks the C library to apply the locale's grouping rules. In the C or POSIX locale there is no grouping character, so the same command prints plain digits. The locale also has to be installed on the system; if it is not, the setting has no effect, and Bash may print a warning about it.
# Grouping with the printf builtin (requires the named locale to be installed)
LC_ALL=en_US.UTF-8 printf "%'d\n" 1234567
# typically: 1,234,567
LC_ALL=de_DE.UTF-8 printf "%'d\n" 1234567
# typically: 1.234.567
LC_ALL=C printf "%'d\n" 1234567
# typically: 1234567 (no grouping in the C locale)
# Fixed decimal places
LC_ALL=C printf "%.2f\n" 3.14159
# typically: 3.14
The outputs shown in comments are the behavior documented for glibc-based Linux systems with those locales available, not results measured for this article. Other C libraries, such as the one used by macOS, and older glibc versions can differ in detail, so run the commands on the system that will produce the real output.
| Method | What it changes | Locale-dependent | Main caveat |
| printf "%'d" | Inserts grouping separators into an integer | Yes, via LC_NUMERIC or LC_ALL | Plain digits in the C locale; locale must be installed |
| printf "%.2f" | Rounds to a fixed number of decimal places and uses the locale's decimal mark | Yes | Input parsing can also follow the locale (see below) |
| numfmt --grouping (GNU coreutils) | Adds locale grouping to numbers read from arguments or standard input | Yes | Not part of every system by default; useful in pipelines |
| sed grouping idiom | Inserts commas with a regular expression | No | Fixed comma separator; relies on GNU sed syntax |
| LC_ALL=C | Forces period decimal mark and no grouping | Forces a known state | Not localized; intended for machine-readable output |
# numfmt adds grouping in a pipeline
echo 1234567 | LC_ALL=en_US.UTF-8 numfmt --grouping
# Locale-independent comma grouping with GNU sed
echo 1234567 | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta'
Decimal separators and reading input
Locale settings can affect how printf reads its arguments, not only how it writes them. Bash's printf converts floating-point arguments with C library routines that follow LC_NUMERIC, so in a locale whose decimal mark is a comma, an argument written as 3.14 may be rejected as an invalid number or read incorrectly. The safe pattern is to keep data in the C locale while calculating and parsing, and apply a regional locale only to the final printf that produces human-facing text.
# Parse and compute in a predictable locale, format for display at the end
value=$(LC_ALL=C awk 'BEGIN { printf "%.2f", 1234567.891 }')
LC_ALL=de_DE.UTF-8 printf "%'.2f\n" "${value/./,}"
The second line swaps the period for a comma so that a comma-decimal locale can read the value. Whether this round trip produces the expected result depends on the locale and C library in use, so treat it as a pattern to test rather than a guaranteed output.
Will the result stay readable and usable after copying or sharing?
- Copy and paste: the separators are real characters, so they travel with the text. The reader sees exactly what was printed, which is the point for reports and messages.
- Re-use in Bash: arithmetic expansion such as $(( n + 1 )) will not accept 1,234,567. Remove separators first, for example with ${n//,/}, or keep the raw value in a separate variable.
- CSV and other delimited files: a comma used as a grouping separator collides with the field delimiter unless the field is quoted, and a comma decimal mark causes the same problem.
- Search and comparison: a search for 1234567 will not match 1,234,567, and string comparisons treat differently formatted values as different text.
- Ambiguity across regions: 1.234 means about one thousand in some locales and just over one in others. Output shared with an international audience can be misread unless the locale is stated or plain digits are used.
- Invisible characters: some locales group digits with a space character, and depending on the C library version this may be a non-breaking or narrow non-breaking space rather than an ordinary space. These can look identical to a normal space but break tools that split on spaces or match patterns. Piping the output through od -c shows which bytes were produced.
- Assistive technology: no documentation or scoped test was supplied on how specific screen readers announce grouped numbers. A reasonable expectation is that separators matching the reader's own locale are announced naturally while unfamiliar ones may be read differently, but this should be checked with the actual screen reader and language settings before relying on it.
A plain-text equivalent is needed whenever the number has a second life beyond being read: logs that will be grepped, values written to files for other programs, data sent to APIs, and anything a later script will compute with. Printing both forms, or keeping the unformatted value alongside the display string, avoids having to strip separators later.
When plain digits are the better choice
- The output is consumed by another program, script or spreadsheet import.
- The script runs on machines whose installed locales or C library you do not control, such as containers and CI runners, which often provide only the C locale.
- The audience spans regions with conflicting separator conventions and the locale is not stated.
- Values are stored in configuration, CSV, JSON or databases.
- Exact reproducibility matters, for example in tests that compare output text.
Practical checks before relying on it
# Is the locale installed?
locale -a | grep -i 'de_DE'
# Which locale settings are active?
locale
# Which bytes does the formatted output actually contain?
LC_ALL=fr_FR.UTF-8 printf "%'d\n" 1234567 | od -c
Run these on the system that will produce the output, confirm the separator characters with od, and check that any later step can still parse the value or has access to an unformatted copy.
Evidence gaps
No tested outputs, platform comparisons or accessibility test results were supplied for this review. The behavior described reflects documented printf, locale and coreutils conventions; exact output, especially for space-based separators, comma-decimal input parsing and non-glibc systems, should be confirmed locally, and screen reader behavior should be verified with the specific product and language settings in use.