0

Could someone please help me out with loop problem. I need it to display the total of all the numbers. This is what i have tried so far:

sum=0  
for numbers in 6 9 -4 7   
do   
   sum=$(($sum+$numbers))    
echo $sum 
done

The output shows these numbers:

6  
15   
11   
18
1
  • Are you only wanting to echo the final output? Don't print inside the loop, and just add to $sum. echo $sum outside the loop. Commented Mar 29, 2014 at 17:44

1 Answer 1

1

Move the echo $sum outside of the for loop to display the final value, instead of displaying its running value from within the loop.

sum=0
for numbers in 6 9 -4 7
do
sum=$(($sum+$numbers))
done
echo $sum
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.