I have been having issues summing a very big array (millions of numbers), and was trying to sum all the values inside but it keeps failing (giving me 0 from the initial component). Below is my code:
Map.sh
#/bin/bash
file="myfile.csv"
data=`tail -n +2 $file | cut -d"," -f 4`
data1=()
for i in $data;
do
data1+=($i)
done;
count=${#data1[@]}
export count
export data1
export data
./reduce.sh
reduce.sh
#/bin/bash
echo $count
sum=0
for i in "${data1[@]}"; do
sum = $((sum + $i))
done;
echo $sum
I have tried almost every single variable I have found online but none works. Am I missing something?
data example:
I am looking at this column (4):

and it extends by millions.
data1array? BTW, what is the purpose of turning your shell variables into environment variables? Aside from the fact that a bash array can not be exported, you don't have any child process which would benefit from the export.sum = $((sum + $i))is wrong (blanks around=); shellcheck.net tells you things like that.sum=$((sum+10#i))or with bash:sum+=$((10#i)). Anyway using a shell to iterate over a large data set is not appropriate.read -r sum < <(IFS='+'; printf '%s\n' "${data1[*]}" | bc -l)orread -r sum < <(tail -n +2 "$file" | cut -d ',' -f 4 | tr '[:space:]' '+' | bc -l)