views:

649

answers:

6

I have to variables and I want to find the value of one divided by the other. What commands should I use to do this?

A: 

I assume that by Linux console you mean Bash.

If X and Y are your variables, $(($X / $Y)) returns what you ask for.

Paolo Capriotti
You don't need the $'s for the variables
Draemon
+2  A: 

In the bash shell, surround arithmetic expressions with $(( ... ))

$ echo $(( 7 / 3 ))
2

Although I think you are limited to integers.

Dave Hinton
Yes, bash is limited to integer math.
Matt Kane
A: 

Example of integer division using bash to divide $a by $b:

echo $((a/b))
Draemon
+1  A: 

In bash, if you don't need fractions in your division, you can do:

>echo $((5+6))
11
>echo $((10/2))
5
>echo $((10/3))
3
Mark Rushakoff
+1  A: 

I still prefer using dc, which is an RPN calculator, so quick session to divide 67 by 18 with 4 digits precision would look like

>dc
4k
67
18/p
3.7222
q
>

Obviously, much more available: man dc

libjack