Sum of all Array Elements in Linux

In this Blog, Sharing the code to add all elements of an Array using shell scripting.

a =(1 2 3 4) ## Array of 4 Elements
i=0
len=${#a[@]} ## It gives the length of an Array
sum=0 ## Setting sum variable to value 0
while [ $i -lt $len ]
do
sum=$((${a[i]}+$sum))
i=$(( $i + 1))
done
echo $sum

Related posts