0

Please assist on that matter, I am trying to get a total number from loop that outputs 4 numbers as shown below:

#!/bin/bash
Bank=4
for ((out=1; out<=$Bank; out++)) do
        echo $out
done

I am getting output that looks like that:

1
2
3
4

How can I calculate 1+2+3+4 and get that calculated output 10 instead of 1 2 3 4? Originally variables 1 2 3 4 will be different like 585 430 170 64, can't figure out where to put | bc -l. Thanks!

2
  • Add to your loop: sum=$(($sum+$out)) and output $sum after your loop. Commented Sep 26, 2022 at 20:24
  • This option is very helpful as well, Thanks! Commented Sep 26, 2022 at 20:54

1 Answer 1

2

where to put | bc -l

After the loop.

for ....
  ...
done | paste -sd+ | bc -l
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.