脚本内的printf命令返回“无效编号",而返回“无效编号". [英] printf command inside a script returns "invalid number"

查看:355
本文介绍了脚本内的printf命令返回“无效编号",而返回“无效编号".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bash脚本中,我无法获得printf来打印带有%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还是比较陌生,并且经常使用echo.另外,我在任何地方都找不到答案.

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"

仅在当前Shell进程的持续时间内设置$LC_NUMERIC.

sets $LC_NUMERIC only for the duration of the current shell process.

如果添加

export LC_NUMERIC="en_US.UTF-8"

$HOME/.bashrc$HOME/.bash_profile,它将为您启动的所有bash shell设置$LC_NUMERIC.

to your $HOME/.bashrc or $HOME/.bash_profile, it will set $LC_NUMERIC for all bash shells you launch.

查找在.bashrc或其他Shell启动文件中设置$LC_NUMERIC的现有代码.

Look for existing code that sets $LC_NUMERIC in your .bashrc or other shell startup files.

这篇关于脚本内的printf命令返回“无效编号",而返回“无效编号".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆