I am trying to create a script where the sum of the array is what prints but my results just end up showing me "Sum: 0"
Here is my script so far.
Any help in correcting this would be appreciated.
#! /bin/bash
declare -a array1={1 2 3 4}
#read array
read -a array1
#set sum to zero
sum=0
#loop for sum in array
for i in ${array1[@]}; do
let sum+=$i
done
#print
echo "Sum: $sum"
declare
doesn't work, array assignments take parenthesis, not braces, though you're not using the assigned array anyway, since youread
over it. Also you should quote the array expansion,"${array1[@]}"
. But other than that, it does count the sum of the values entered, I don't see a problem in that. – ilkkachu Jan 09 '18 at 00:21