当我在我的 shell 中运行如下命令时,它会返回一个 expr: non-integer argument
错误。有人可以向我解释一下吗?
$ x=20
$ y=5
$ expr x / y
expr: non-integer argument
最佳答案
这些变量是shell变量。要将它们作为参数扩展到另一个程序(ie expr
),您需要使用 $
前缀:
expr $x / $y
它提示的原因是因为它认为你正在尝试对字母字符进行操作(ie 非整数)
如果您使用的是 Bash shell,则可以使用表达式语法获得相同的结果:
echo $((x / y))
或者:
z=$((x / y))
echo $z
https://stackoverflow.com/questions/18093871/