shell - 乘以2个数组索引变量shell编程

标签 shell debugging ubuntu command-line

给定以下内容,我如何将 2 个数组索引变量相乘

foo=(1 2)
bar=(0.1 0.2)

foobar=$((foo[1]*price[1])) # this is wrong

echo "$foobar"

当前输出:0
正确/预期输出:0.4

最佳答案

bash 只做整数运算。对于浮点数学,通常的做法是使用 bc :

$ foobar=$(echo "${foo[1]} * ${bar[1]}" | bc)
$ echo $foobar
.4

请注意取消引用数组元素所需的语法:需要大括号将变量名称和索引组合在一起。

关于shell - 乘以2个数组索引变量shell编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21047849/

相关文章:

linux - zmap 命令后的 Cat

debugging - 尝试调试时Visual Studio 2015停止工作

ubuntu - 在 Linux 上使用 QT creator 编译基于 portaudio 的 C 代码

python - 无法使用 cv2.imread() 读取图像

MySQL Server 频繁关闭,现在不会重新启动

shell - 如何在 diff 查看器仍然打开的情况下让cleartool diff 返回到命令行?

shell - 使用 sh shell 比较字符串

linux - 如何使此 "action-packed, random data"在终端中回显?

debugging - 崩溃调试Win32应用程序

debugging - Visual Studio 2015 .NET Core 1.0 自动构建