kman99
kman99

Reputation: 7665

How do I divide in the Linux console?

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?

Upvotes: 78

Views: 116996

Answers (12)

SHIBLI
SHIBLI

Reputation: 11

I also had the same problem. It's easy to divide integer numbers but decimal numbers are not that easy. if you have 2 numbers like 3.14 and 2.35 and divide the numbers then, the code will be Division=echo 3.14 / 2.35 | bc echo "$Division" the quotes are different. Don't be confused, it's situated just under the esc button on your keyboard. THE ONLY DIFFERENCE IS THE | bc and also here echo works as an operator for the arithmetic calculations in stead of printing. So, I had added echo "$Division" for printing the value. Let me know if it works for you. Thank you.

Upvotes: -1

lsiebert
lsiebert

Reputation: 677

you can also use perl -e

perl -e 'print 67/8'

Upvotes: 1

JADAV AKASH
JADAV AKASH

Reputation: 9

You should try to use:

echo "scale=4;$variablename/3"|bc

Upvotes: 1

jperrie
jperrie

Reputation: 171

You can use awk which is a utility/language designed for data extraction

e.g. for 1.2/3.4

>echo 1.2 3.4 | awk '{ print $2/$1 }'
0.352941

Upvotes: 14

2upmedia
2upmedia

Reputation: 2910

Something else you could do using raytrace's answer. You could use the stdout of another shell call using backticks to then do some calculations. For instance I wanted to know the file size of the top 100 lines from a couple of files. The original size from wc -c is in bytes, I want to know kilobytes. Here's what I did:

echo `cat * | head -n 100 | wc -c` / 1024 | bc -l

Upvotes: 2

raytrace
raytrace

Reputation: 431

echo 5/2 | bc -l

2.50000000000000000000

this '-l' option in 'bc' allows floating results

Upvotes: 43

user1504475
user1504475

Reputation: 180

Better way is to use "bc", an arbitrary precision calculator.

variable=$(echo "OPTIONS; OPERATIONS" | bc)

ex:

my_var=$(echo "scale=5; $temp_var/100 + $temp_var2" | bc)

where "scale=5" is accuracy.

man bc 

comes with several usage examples.

Upvotes: 17

Mark Rushakoff
Mark Rushakoff

Reputation: 258368

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

>echo $((5+6))
11
>echo $((10/2))
5
>echo $((10/3))
3

Upvotes: 10

libjack
libjack

Reputation: 6443

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

Upvotes: 11

Draemon
Draemon

Reputation: 34711

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

echo $((a/b))

Upvotes: 2

dave4420
dave4420

Reputation: 47062

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

$ echo $(( 7 / 3 ))
2

Although I think you are limited to integers.

Upvotes: 74

Paolo Capriotti
Paolo Capriotti

Reputation: 4072

I assume that by Linux console you mean Bash.

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

Upvotes: 5

Related Questions