Complex calulation in shell script

Hi ,

I need one help to do some complex calculation in shell script.
here is what i need to do:-
a=2
b=2
c=2
d=2
result=a+(b/(20*c))+(c/(10*d))

is there any thing special there so that i can group intermdiate results.

Please help me if you have any idea.

You have to use bc command and also during variable interpolation use $before variable name.

 
echo "$a+($b/20*$c)+($c/10*$d)" | bc

1 Like

You may try

result=$(((a+(b/20*c)+(c/10*d))))

If you have a specific need to group the intermediate results, you may follow the same logic to calculate the intermediate results.

If you need accuracy to the decimal precision, then use awk with printf.

1 Like

Thanks a lot ..it worked :):slight_smile:

If you are using a 1993 or later version of the Korn shell and need a floating point result, you could also use:

result=$((a+(b*1.0/(20*c))+(c*1.0/(10*d))))
echo $result

to produces:

2.15