Linux and Networking

Linux and Networking

4.10. Using bash as a calculator

With bash we can solve arithmetic expressions in our scripts without problems as long as they are not very complex (if they are complex it is better to use bc). If for example we want to execute the following in a script or in a terminal:

 

$ echo 1+1

1+1

 

The result is what you can see. The bash command interpreter understands it as if it were a string and will never display 2. But if we do the following:

 

$ echo $((1+1))

2

 

or if we do the following:

 

$echo $[1+1]

2

 

Yes it will work. Whenever you work with integers you can use the previous expressions. That said, for more complex expressions you have to use bc.

The Demeter Project