1

Using a bash script, I need to print to the terminal a string that uses a variable that's storing a number, but for the variable to print with comma separation if it needs it.

So for example, if I had a variable with a value of 1000, I would want to print to the terminal something like:

"Count is equal to 1,000"

I'm trying to figure out if I can do this with a single printf command. Is there a formatting option for printf that can do this, or is there a way to manipulate the variable such that I can turn it into a string with comma separation?

  • Interesting question but `print with comma separation if it needs it.` is not a good explanation. When should comma be printed? – Arkadiusz Drabczyk Sep 05 '19 at 19:53
  • 1
    @ArkadiuszDrabczyk You're right, my bad. When I said "if it needs it", what I meant is if the number variable would need comma separation to divide up units. Such as for 1,234 and 1,234,567 but not for 123 – Dustin Henson Sep 05 '19 at 20:25

1 Answers1

1

Try this:

printf "%'d" $MYVAR

If you need a new line at the end, do:

printf "%'d\n" $MYVAR

All together:

printf "Count is equal to %'d\n" $MYVAR

If you're on a Mac, you might need to install coreutils. I'd suggest doing that with Homebrew: brew install coreutils.

Per @KamilCuk, it's important to note that comma/decimal separation with numbers is based on your localization. If you'd like to change that, you can do so per this answer: https://stackoverflow.com/a/12845640/6246128

wcarhart
  • 2,685
  • 1
  • 23
  • 44
  • 1
    Note that the thousands separator and the count of digits in a group is locale dependent. – KamilCuk Sep 05 '19 at 20:17
  • 1
    `printf` is a bash/zsh builtin, so just installing coreutils to get GNU `printf` won't be effective. If the formatting modifier doesn't work, it's probably because of the old version of bash. If you do install `coreutils`, you'll need to make sure that you invoke the external command `printf` rather than the builtin. – rici Sep 05 '19 at 20:28
  • bash's builtin `printf` does support this format, and locales. But the bash version that comes with macOS (v3.2.57) seems to have a bug where using a locale setting as a prefix to the `printf` command (e.g. `LC_ALL=en_US printf "%'d\n" $MYVAR`) doesn't work right. But making a function-local locale setting (like `printcomma() { local LC_ALL=en_US; printf "%'d\n" "$1"; }`) and forcing a subshell and setting it there (like `(LC_ALL=en_US; printf "%'d\n" "$MYVAR")` ) both work. – Gordon Davisson Sep 05 '19 at 22:00
  • I don't have a Mac, but wouldn't using awk be an effective workaround? eg. `awk "BEGIN{printf \"%'d\n\",ARGV[1]}" 1000000`. –  Sep 05 '19 at 22:59
  • @mosvy That'd work, although again you might need to force a locale that uses commas for thousands: `LC_ALL=en_US awk "BEGIN{printf \"%'d\n\",ARGV[1]}" "$MYVAR"` – Gordon Davisson Sep 06 '19 at 00:00
  • @GordonDavisson my impression was that awk would turn its printf into multiple calls to the libc's printf (rather than doing its own format parsing), which _should_ be a bit more robust & more complete wrt locales and format support. That's why I suggested it as a workaround for a buggy printf shell built-in. –  Sep 06 '19 at 00:55