Unexplicable错误在bash:一个脚本返回&QUOT里面的printf命令;无效号码" [英] Unexplicable error in bash: printf command inside a script returns "invalid number"
问题描述
我不能让的printf
打印变量,在bash脚本中的%E描述符。它只是说
I can't get printf
to print a variable with the %e descriptor in a bash script. It would just say
#!/bin/bash
a=14.9
printf %e 14.9;
我知道这可能是一个非常简单的问题,但我是相当新的bash和总是使用回声
。另外,我找不到任何地方的答案。
I know this is likely a very easy question, but I'm fairly new to bash and always used echo
. Plus I couldn't find an answer anywhere.
在运行时我得到
$ ./test.text
./test.text: line 3: printf: 14.9: invalid number
0,000000
所以我的问题是语言环境变量LC_NUMERIC:它被设置,这样我用逗号作为小数点分隔符。实际上,它被设置为一个欧洲的定位
therefore my problem is the locale variable LC_NUMERIC: it is set so that i use commas as decimal separators. Indeed, it is set to an european localization:
$ locale | grep NUM
LC_NUMERIC="it_IT.UTF-8"
我想我将它设置为en_US.UTF-8,但很显然,我没有。现在的问题切换到找到如何设置我的语言环境变量。只需用
I thought I set it to en_US.UTF-8, but evidently I didn't. Now the problem switches to find how to set my locale variable. Simply using
$ LC_NUMERIC="en_US.UTF-8"
将无法工作。
推荐答案
这
LC_NUMERIC="en_US.UTF-8" printf %e 14.9
设定 $ LC_NUMERIC
只为一个命令的持续时间。
sets $LC_NUMERIC
only for the duration of that one command.
这
export LC_NUMERIC="en_US.UTF-8"
设定 $ LC_NUMERIC
仅在当前shell进程的持续时间。
sets $LC_NUMERIC
only for the duration of the current shell process.
如果您添加
export LC_NUMERIC="en_US.UTF-8"
你的 $ HOME / .bashrc中
或 $ HOME / .bash_profile中
,它将设置 $ LC_NUMERIC
所有的bash shell里,您启动。
to your $HOME/.bashrc
or $HOME/.bash_profile
, it will set $LC_NUMERIC
for all bash shells you launch.
查找现有code,设置 $ LC_NUMERIC
在的.bashrc
或其他shell启动文件。
Look for existing code that sets $LC_NUMERIC
in your .bashrc
or other shell startup files.
这篇关于Unexplicable错误在bash:一个脚本返回&QUOT里面的printf命令;无效号码"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!