Consider:
t=0 ; for i in 1 2 3 4 5 6 7 8 9 10 ; do t=$((t+i)) ; done ; echo $t
prints 55.
But:
totsize=0
find /home/user -type f -mmin -4860 -a -mmin +3420 | xargs du | \
while read size rest ; do
totsize=$((totsize+size))
echo "$totsize"
done
echo "Sum: $totsize kb"
Prints "Sum: 0 kb" even tho the interim print statement prints a reasonable sum.
I know I have encountered this issue before, but have never understood it. What is difference?
totsize=0
while read size rest ; do
totsize=$((totsize+size))
echo "$totsize"
done < <(find /home/user -type f -mmin -4860 -a -mmin +3420 | xargs du)
echo "Sum: $totsize kb"
Prevent a subshell, because a subshell will limit the scope of totsize
Spending a few more words:
do_something < <(subprocess)
.
subprocess | do_something
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With