Using an arithmetic expansion

We can use two different ways for evaluating arithmetic expressions:

    $(( expression ))
    $[ expression ]
  

Learn arithmetic operations using the preceding mentioned arithmetic expansion:

    $ a=10
    $ b=20
    $ c=$((a + b))
    $ echo $c  

During arithmetic operations, we may need to find the square or cube of any given number. These operations are called exponent operations. We can perform exponent operations as follows:

    $ a=5
    $ b=3
    $ expo=$[ $a ** $b ]    # This is equivalent to ab
    $ echo $expo
    125  

This is the result of the 53 operations:

Another way to do arithmetic expansions is as follows:

    $ B=10
    $ A=$[B + 10]
    $ echo $A
    20
    $ echo $[ 3 + 4 - 5 ]
    2
    
    $ echo $[ 3 + 4 * 5]
    23  

Arithmetic multiplication has precedence over addition. ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.