Can I do basic maths in Bash?

33

8

I was wondering, is it possible to do simple maths in bash? I'm thinking something like, =25-5 would print out 20 or something.

Can this be done easily?

Thank you

Kurru

Posted 2011-03-11T00:05:48.883

Reputation: 1 476

Answers

22

Just type bc into the terminal. Then type all the math stuff in after that.

bc stands for "basic calculator"

Then type quit and enter to exit.

DCIndieDev

Posted 2011-03-11T00:05:48.883

Reputation: 812

2Just for clarity it would be like that : echo 25-5 | bc – Medhat Helmy – 2016-05-13T09:17:09.087

For some math problems, you may need to wrap it in quotes, such as echo '0.6 * 12' | bc – Goose – 2017-01-26T17:09:42.807

you might also need to add scale to get floating point precision working i.e. echo "scale=2;557/3672" | bc – CpILL – 2019-10-29T23:21:22.087

64

If we are really talking about Bash, not Bourne Shell (sh) or other shells, it's easy.

Bash can compute basic expressions with $((expression)) and here's an example on how you might like to use it:

 a=3
 b=4
 c=$((7*a+b))
 echo $c

or for interactive use, just

 echo $((7*3+4))

Seikku Kaita

Posted 2011-03-11T00:05:48.883

Reputation: 651

3This should've been the accepted answer. – Andreas Hartmann – 2017-09-30T12:03:18.897

It does seem to be proper bash, since that works. I am ssh-ing into one of my universities clusters – Kurru – 2011-03-11T00:59:14.937

4The $((expression)) syntax is part of the POSIX sh standard, and derived from ksh. – geekosaur – 2011-03-11T01:05:40.563

6Bash can only do integer arithmetic. It cannot do floating point arithmetic like ksh93 or zsh – fpmurphy – 2011-03-11T02:27:33.167

11

There are a number of command-line utilities for doing simple calculations:

$ expr 100 \* 4
400

$ echo '100 * 4' | bc
400

to name just two of them. Be careful doing multiplication as if you don't escape your * the shell may try and interpret it as a wildcard.

Majenko

Posted 2011-03-11T00:05:48.883

Reputation: 29 007

10

Another is AWK:

awk 'BEGIN {print 4 + 3 / 12}'

Paused until further notice.

Posted 2011-03-11T00:05:48.883

Reputation: 86 075

7

Well your question is answered, but consider this:

Most of the linux distros have python preinstalled, so why not use it?

Just type

python

in the terminal and then do all the arithmetic you want, like

2+2

Will output 4 :)

You can also do this directly from terminal with the -c python argument.

python -c 'print 2+2'

Marcus Maxwell

Posted 2011-03-11T00:05:48.883

Reputation: 526

use python3 if you don't want float rounding. e.g. 2 / 30 (python: 0) (python3: 0.06666666666666667) – hrvoj3e – 2018-08-28T11:52:55.227

and if you only have python3 installed, but want rounding, use 2//30 or int(2/30) – mazunki – 2019-09-08T16:32:41.120

On my computer, typing python takes nearly two seconds to start. Rather annoying if you just want to do something simple like 2+2. – ShreevatsaR – 2011-03-11T07:14:59.003

3

Or Ruby. :)

Although it may not come pre-installed, it is pretty quick.

Type irb, then 2+2.

Or just ruby -e 'p 2+2'

whirlwin

Posted 2011-03-11T00:05:48.883

Reputation: 637

0

Perl is another option:

perl -E 'say 1/7'

outputs

0.142857142857143

Paused until further notice.

Posted 2011-03-11T00:05:48.883

Reputation: 86 075