3

I want to perform some mathematical operations in the shelll. For example:

5+50*3/20 + (19*2)/7

I tried:

 #!/bin/bash
 read equ
 echo "scale=3; $equ" | bc -l

Expected output:

 17.929

My output:

 17.928
5

bc is truncating, try this instead:

printf "%.3f\n" $(echo "$equ" | bc -l)
0

You can also use perl -e "print $expr",

e.g. perl -e "print 5+50*3/20 + (19*2)/7" outputs 17.9285714285714

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.