Bash Shell scripting – Arithmetic Expressions in BASH




The Arithmetic expression is very important feature for performing number arithmetic operations in scripts.
By default variables are treated as strings in bash scripts.
But parsing a string to numbers is very easy using double parentheses and external command such as expr.

Arithmetic operations in Bash Shell
Arithmetic operations can be done on integers using the following methods

$(( expression ))

Example:
Run the following commands on terminal:

You can also create a bash script for the above operations. For example we have a script operations.sh

now run the above script

Output:

expr — evaluate expression
we can also use expr command to perform Arithmetic operations on numbers.
For example we have a script operations_expr.sh.

now run the above script

Output:


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com