带变量的bash算术表达式 [英] bash arithmetic expressions with variables
本文介绍了带变量的bash算术表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在bash文件(unix文件.sh)中的算术表达式上遇到麻烦.
I am having troubles with the arithmetic expressions in a bash file (a unix file .sh).
我有一个变量"total",它由一些用空格隔开的数字组成,我想计算它们的总和(在变量"dollar"中).
I have the variable "total", which consists of a few numbers separated by spaces, and I want to calculate the sum of them (in the variable "dollar").
#!/bin/bash
..
dollar=0
for a in $total; do
$dollar+=$a
done
我知道我在算术括号中缺少某些内容,但无法使它与变量一起使用.
I know I am missing something with the arithmetic brackets, but I couldn't get it to work with variables.
推荐答案
在((...))
中包装算术运算:
dollar=0
for a in $total; do
((dollar += a))
done
这篇关于带变量的bash算术表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文