Loading...
Shell Script

Arithmetic Substitution

In ksh and bash, the shell enable integer arithmetic to be to be performed.This avoids having to run an extra program such as expr or bc to do math in a shell script

Syntax:  Var=$((expression))

Example:
$ foo=$((((2+6*3)-5)/3))
$ echo $foo
5

The operators which can be used in the arithmetic substitution.

Operators      Description
/                The division Operator divides two numbers and return the result.
*                The multiplication operator multiplies two numbers and return the result.
–                 The subtraction operator subtracts two numbers and returns the difference.
+                 The addition operator adds two numbers and returns the result.
()                 The parentheses is used to clarify which expression should be evaluated first.

Leave a Reply

Your email address will not be published. Required fields are marked *