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
17 Answers
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.
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))4
There are a number of command-line utilities for doing simple calculations:
$ expr 100 \* 4
400
$ echo '100 * 4' | bc
400to 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.
Another is AWK:
awk 'BEGIN {print 4 + 3 / 12}' Well your question is answered, but consider this:
Most of the linux distros have python preinstalled, so why not use it?
Just type
pythonin the terminal and then do all the arithmetic you want, like
2+2Will output 4 :)
You can also do this directly from terminal with the -c python argument.
python -c 'print 2+2' 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'
Perl is another option:
perl -E 'say 1/7'outputs
0.142857142857143