Performing Integer Math with Expressions
You can do integer math directly in bash
, which is sometimes handy. But, bash
doesn’t have the capability of doing floating point math. For that, you’ll need to use a separate utility, which we’ll look at later.
If you ever try to use echo
to perform math on the command-line, you’ll find that it doesn’t work. What you’ll get will look something like this:
[donnie@fedora ~]$ echo 1+1
1+1
[donnie@fedora ~]$ echo 1 + 1
1 + 1
[donnie@fedora ~]$
This is because echo
treats your math problem as just a normal text string. So, you’ll need some other way to solve your math problems. Fortunately, there are a few different ways to do this.
Using the expr Command
The expr
command is for evaluating expressions. These expressions can be normal text strings, regular expressions, or mathematical expressions. For now, I’ll just talk about using it to evaluate math expressions. Here...