78

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?

gizmo
  • 11,819
  • 6
  • 44
  • 61
kman99
  • 7,315
  • 4
  • 24
  • 20
  • 1
    Is this really programming related? [http://stackoverflow.com/faq] – Frank V Jul 06 '09 at 17:26
  • 3
    Check this article: [http://www.basicallytech.com/blog/index.php?/archives/23-command-line-calculations-using-bc.html](http://www.basicallytech.com/blog/index.php?/archives/23-command-line-calculations-using-bc.html) – freitass Jul 06 '09 at 17:08
  • 1
    @frank-v Bash can be used for programming... also, that link to faq doesn't work – chomp Jun 18 '16 at 01:52

12 Answers12

74

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

$ echo $(( 7 / 3 ))
2

Although I think you are limited to integers.

dave4420
  • 46,404
  • 6
  • 118
  • 152
43
echo 5/2 | bc -l

2.50000000000000000000

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

raytrace
  • 431
  • 4
  • 2
  • 3
    Cool; with `-l`, you get 20 decimal places by default *and* a math library is loaded that defines additional functions such as sine; to explicitly specify the number of decimal places, prepend the expression with `scale={numPlaces};` (too), as demonstrated in @user1504475's answer. In other words: `-l` is a convenient alternative to specifying `scale=20;`, but comes at a performance cost due to loading the library - the real-world impact appears to be negligible, though. – mklement0 Oct 27 '13 at 21:54
17

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.

Pascail
  • 374
  • 2
  • 11
user1504475
  • 180
  • 1
  • 3
  • 1
    Cool; one thing to note: not setting `scale` defaults to integer division - unless you specify `-l`, as @raytrace notes below, which apparently gives you 20 decimal places by default, but also loads a library with additional functions. – mklement0 Oct 27 '13 at 21:38
14

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
dmportella
  • 4,614
  • 1
  • 27
  • 44
jperrie
  • 171
  • 1
  • 3
11

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
  • 6,403
  • 2
  • 28
  • 36
  • 4
    A few pointers to possibly save some a trip to `man`: RPN = Reverse Polish Notation; in a nutshell: you enter the operands first, then the operator. A terminating `p` is needed to output the result. The non-interactive version of the above calculation is: `dc -e '4k 67 18 / p'`. Caveat: not setting the precision (with `{numPlaces}k`) defaults to 0, i.e., integer division. – mklement0 Oct 27 '13 at 21:39
10

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
Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
5

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
  • 4,052
  • 21
  • 25
2

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

echo $((a/b))
Draemon
  • 33,955
  • 16
  • 77
  • 104
2

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
2upmedia
  • 2,832
  • 1
  • 20
  • 16
1

You should try to use:

echo "scale=4;$variablename/3"|bc
fivedigit
  • 18,464
  • 6
  • 54
  • 58
1

you can also use perl -e

perl -e 'print 67/8'
lsiebert
  • 667
  • 1
  • 5
  • 16
-1

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.

SHIBLI
  • 11
  • 1
  • The quotes are not showing here, So, just put the quotes(``) after = and after bc. – SHIBLI Nov 04 '17 at 19:54
  • Those are not quotes. They are acute accents. They are not showing because StackOverflow uses them to highlight code snippets `some code here` was just wrapped in the acute accents. – Chris Sharp Nov 04 '17 at 20:27