Multiplication on command line terminal

bouncingHippo picture bouncingHippo · Jun 14, 2012 · Viewed 200k times · Source

I'm using a serial terminal to provide input into our lab experiment. I found that using

$ echo "5X5"

just returns a string of "5X5". Is there a command to execute a multiplication operation?

Answer

KurzedMetal picture KurzedMetal · Jun 14, 2012

Yes, you can use bash's built-in Arithmetic Expansion $(( )) to do some simple maths

$ echo "$((5 * 5))"
25

Check the Shell Arithmetic section in the Bash Reference Manual for a complete list of operators.

For sake of completeness, as other pointed out, if you need arbitrary precision, bc or dc would be better.