Unix / Linux: Bash Number Currency Formatting Thousands Grouping Separator

In a bash shell, you can format numbers with a currency-style thousands grouping separator by using the printf command with the %' format specifier.

For example, to format a number with a comma as the thousands grouping separator, you can use the following syntax:

printf "%'d\n" <number>

Replace <number> with the number you want to format.

Here’s an example that formats the number 1234567:

printf "%'d\n" 1234567

This will produce the following output:

1,234,567

Note that the %' format specifier is locale-specific, so the character used as the thousands grouping separator may differ based on your locale setting. To ensure that the correct separator is used, you may need to set the LC_NUMERIC locale to a specific value, for example:

LC_NUMERIC=en_US.UTF-8 printf "%'d\n" 1234567

Leave a Comment