I'm using bash and trying to add all elements of an array that was created from a file.
while read line; do
array=($line);
sum=0
length=${#array[@]}
for i in ${array[@]:0:$length}; do
sum=$[$sum+${array[i]}] #<--- this doesn't work?
done
echo $sum
done < $1
edit: I should have been clearer why i want to use array splitting in for loop
The input may be ------> david 34 28 9 12
And I want to print ---> david 83
So I would like to loop through all elements accept the first one. so i would use:
length=$[${#array[@]} - 1]
for i in${array[@]:1:$length}
because of this i can't use:
for i in "${array[@]}"