How do I auto-increment and auto-decrement in a bash shell script?



  • The post and pre increment and decrement arithmetic operators. ++ & -- The pre and post determine if the value is returned before or after.

    This applies to auto-decrement as well.
    i++ gets the current value of i before and then increments it.
    ++i increments variable i and then uses the result after.

    StartNumber=0
    
    while (($StartNumber < 10))
    do
      StartingNumber=$(($StartNumber < 10))
      let "++StartNumber"
      echo $StartNumber
    done
    

    Running this give the following output:

    $ ./arithmetic.sh
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    The let command is not the only way to increment or decrement.


Log in to reply
 

© Lightnetics 2024