Look at the following script:
Code:
#!/bin/bash
#
# Create the array
#
declare -a array
for i in {1..6}
do
array[$i]=${i}
done
#
# Sum array's elements
#
sum=0
for i in {1..6}
do
sum=$(($sum + ${array[$i]}))
done
echo $sum
If you have floating point numbers, take in mind that bash can't manage them, so that you have to use bc or awk. Here is an example using bc:
Code:
#!/bin/bash
#
# Create the array
#
declare -a array
for i in {1..6}
do
array[$i]=${i}.2
done
#
# Sum array's elements
#
sum=0
for i in {1..6}
do
sum=$(echo "$sum + ${array[$i]}" | bc)
done
echo $sum