Chen Xie Chen Xie - 1 year ago 88
Bash Question

Shell: how to evaluate an arithmetic expression with more than two operands

I have read several posts but they are talking about two operands

I have got the following expression that is to be evaluated:

sum = a / b * 100

In the bash shell, I can do:

bash-4.1$ a=5
bash-4.1$ b=10
bash-4.1$ sum=`expr $a / $b`
bash-4.1$ echo $sum

However, I cannot do:

bash-4.1$ sum=`expr $a / $b * 100`
expr: syntax error

Answer Source
result=$(echo "($a/$b) * $c)" | bc -l)

Try bc.

For integers: echo $(( 10 /5 *200 ))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download