What methods can be used to do arithmetic evaluations in bash?



  • Man page: bash - GNU Bourne-Again SHell

    You can use the builtin let or ((...))

    For producing the result from the expression use $((...))

    e.g:

    $ mynumber=5
    $ let "mynumber=$mynumber + 1"
    $ echo $mynumber
    6
    

    Help on the let builtin can be obtained using:

    $ builtin help let
    

    Using ((...)) arithmetic evaluation in a bash script, you will see this syntax used in for and while loops.

    StartingNumber=$(($StartNumber < 10))
    


© Lightnetics 2024