Problem
I have two variables and need to discover the value of one when divided by the other. To achieve this, what commands should I use?
Asked by kman99
Solution #1
Surround arithmetic expressions with $((…)) in the bash shell.
$ echo $(( 7 / 3 ))
2
However, I believe you are restricted to integers.
Answered by dave4420
Solution #2
echo 5/2 | bc -l
2.50000000000000000000
‘bc’ has a ‘-l’ option that allows floating results.
Answered by raytrace
Solution #3
Use “bc,” an arbitrary precision calculator, instead.
variable=$(echo "OPTIONS; OPERATIONS" | bc)
ex:
my_var=$(echo "scale=5; $temp_var/100 + $temp_var2" | bc)
where “scale=5” is accuracy.
man bc
Provides includes a number of examples of how to use it.
Answered by user1504475
Solution #4
You can use awk, which is a utility/language for extracting data.
e.g. for 1.2/3.4
>echo 1.2 3.4 | awk '{ print $2/$1 }'
0.352941
Answered by jperrie
Solution #5
I still prefer to use dc, an RPN calculator, so a quick session to divide 67 by 18 with 4 digit precision might look like this.
>dc
4k
67
18/p
3.7222
q
>
Obviously, there’s a lot more: man dc
Answered by libjack
Post is based on https://stackoverflow.com/questions/1088098/how-do-i-divide-in-the-linux-console