Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash, how to calculate total from loop output

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

where to put | bc -l

After the loop.

for ....
  ...
done | paste -sd+ | bc -l
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading