Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/bin/bash printf does not work with other LANG than C

I have a really weird problem with /bin/bash and a script that uses printf to format a string.

My script looks like this

rt=$(printf "%.3f" 13.234324245)

with the difference, that i compute the number 13.23... above. When i use /usr/bin/zsh that works great! Even /bin/sh can do it (but it cant do the if stuff...) The biggest problem ist that /bin/bash seems to does not understand printf or does have another formating way when i dont use LANG=C.

My LANG Variable is set to de_AT.UTF-8 and then i get this Error:

/path/to/script: Zeile 12: printf: 13.234324245: Ungültige Zahl.

So it simply says that the number i gave printf is invalid...

Do i need to run printf in a different way?

edit: The problem seems to be on the computation of the number:

rt=$(printf "%.3f" $(echo "$res2 - $res1"|bc ))

how i can tell bc to use a , instead of .?

like image 206
reox Avatar asked Jan 13 '12 09:01

reox


1 Answers

Your problem stems for the LC_NUMERIC setting, which bash follows when parsing arguments to printf.

I find this behavior dubious. Ksh93 also parses numbers according to LC_NUMERIC while pdksh and dash want a dot as the separator in the argument, and zsh accepts either a dot or the local format. POSIX doesn't say, because the floating point format specified are optional in printf (LC_NUMERIC must be respected when printing numbers though).

As a user I recommend never setting LC_NUMERIC and LC_COLLATE except in very specific circumstances where you're sure you want their behavior. This means not using LANG and setting specific categories instead; most people only need LC_CTYPE (character encoding), LC_MESSAGES (language of messages) and LC_TIME (date and time format). In a script, you can override all categories by setting LC_ALL, or a specific category by setting it (setting LANG only overrides $LANG and not a user-set $LC_NUMERIC).

#!/bin/sh
LC_NUMERIC=C LC_COLLATE=C
printf "%.3f" 13.234324245
like image 99
Gilles 'SO- stop being evil' Avatar answered Sep 19 '22 02:09

Gilles 'SO- stop being evil'