Compute the Average

  • + 1 comment

    Bash doesn't directly support such operation for Floating-point numbers. You'll need external tools such as awk, bc to get the job done. I would rather keep the code simple in such cases.

    read n
    sum=0
    for ((i=0;i<$n;i++))
    do
        read temp
        sum=$(awk "BEGIN {print $sum+$temp; exit}")
    done
    printf "%.3f\n" $(bc -l <<< "$sum/$n")