total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $total
This outputs:
1 3 6 10 15 0
whereas I am expecting:
1 3 6 10 15 15
My bash version:
> bash -version GNU bash, version 3.1.17(1)-release (x86_64-redhat-linux-gnu)
meharo
Because the behavior of your script is undefined and bash decided to have the left side of a pipeline to be run in the original shell process and not the rightmost, a poor decision IMHO.
Switch to ksh if you want your script to behave as you expect.
a better syntax would be
total=0 for in $(seq 5) ; do total=$(($total+$i)) echo $total done echo $total
That should read
for i in $(seq 5) ...
Have a look here for information about how to get around the pipe thing in bash.