bashmathshevaluation

Bash: evaluate a mathematical term?


echo 3+3

How can I evaluate such expressions in Bash, in this case to 6?


Solution

  • in shells such as zsh/ksh, you can use floats for maths. If you need more maths power, use tools like bc/awk/dc

    eg

    var=$(echo "scale=2;3.4+43.1" | bc)
    var=$(awk 'BEGIN{print 3.4*43.1}')
    

    looking at what you are trying to do

    awk '{printf "%.2f\n",$0/59.5}' ball_dropping_times >bull_velocities