如何在Linux Shell中使用变量进行除法? [英] How can I do division with variables in a Linux shell?
本文介绍了如何在Linux Shell中使用变量进行除法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在下面的shell中运行命令时,它返回expr: non-integer argument
错误.有人可以向我解释一下吗?
When I run commands in my shell as below, it returns an expr: non-integer argument
error. Can someone please explain this to me?
$ x=20
$ y=5
$ expr x / y
expr: non-integer argument
推荐答案
这些变量是shell变量.要将它们作为参数扩展到另一个程序( ie expr
),您需要使用$
前缀:
Those variables are shell variables. To expand them as parameters to another program (ie expr
), you need to use the $
prefix:
expr $x / $y
它抱怨的原因是因为它认为您正在尝试使用字母字符(即非整数)
The reason it complained is because it thought you were trying to operate on alphabetic characters (ie non-integer)
如果使用的是Bash shell,则可以使用表达式语法实现相同的结果:
If you are using the Bash shell, you can achieve the same result using expression syntax:
echo $((x / y))
或者:
z=$((x / y))
echo $z
这篇关于如何在Linux Shell中使用变量进行除法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文