I made an script that do some math, using bc
and printf
.
It worked well under cygwin
which locale is en_US.UTF-8
, but when I run it under linux, which locale is en_ES.UTF-8
, it fails because it uses ,
as decimal separator. For example next expression fails:
avg=$(printf %.2f $(echo "scale=4; $val1/$val2" | bc -l ))
I found a solution. Precede the script by LC_ALL=C.UTF8
:
LC_ALL=C.UTF8 ./script.sh [OPTIONS]
However, I think it would be better not to do that.
So, my question: Is there a way to change locale only inside the script, to avoid such kind of problems, regardless of locale set in user profile?
LC_XXX=YYY
. The only case that it's used in a script is when usingksh93
shell, but it overrides usingtypeset LC_ALL=C
instead ofexport LC_ALL=C
. I'm usingbash
, can I do the same? If so, is there a difference inbash
betweentypeset LC_ALL=C
andexport LC_ALL=C
? – Albert Feb 03 '16 at 12:32