Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to do integer & float calculations, in bash or other languages/frameworks?

Using echo "20+5" literally produces the text "20+5".

What command can I use to get the numeric sum, 25 in this case?

Also, what's the easiest way to do it just using bash for floating-point? For example, echo $((3224/3807.0)) prints 0 :(.

I am looking for answers using either the basic command shell ('command line') itself or through using languages that are available from the command line.
by

2 Answers

akshay1995
There are many ways to calculate. For simple expressions you can use bash itself:

echo $((20+5))

or expr:

expr 20 + 5

And for complex cases there is great tool bc:

echo "20+5" | bc

Btw, bc can calculate even very complex expression with roots, logarithms, cos, sin and so on.
pankajshivnani123
You can use calc:

If you just enter calc with no other arguments it enters an interactive mode where you can just keep doing math. You exit this by typing exit:

C-style arbitrary precision calculator (version 2.12.3.3)
Calc is open software. For license details type: help copyright
[Type "exit" to exit, or "help" for help.]

; 2+4
6
; 3+5
8
; 3.4+5
8.4
; 2^4
16
; exit


Or you use it with the expression as an argument and it will provide the answer and then exit
$calc 2 + 4
6
$

calc is similar to bc, I just like the way it behave as default better

Login / Signup to Answer the Question.