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?
that’s because the pipes create a subshell so totsize is “local” inside that subshell. you can try this instead (bash)
Or instead of using bash, call
awkBut are you sure you want to use just
duwithout any options, because the size is not “accurate” (usingdu -bwould be better). If you have GNU find, you can use-printf