Can I do basic maths in Bash?

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))

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.


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.

Tags:

Bash

Ssh