How do I use the various assignment operators in a bash script?



  • The commonly used assignment operators are =, +=, -=. *=, /=, and %=

    The example here only shows to the += but the same usage applies to all of them.

    These two are written differently but are the same. The first is the short form, and you can do this for all the above assignment operators.

    Both of the following let statements are the same.

    let "a += 4"
    
    let "a = a / 4"
    

    There are some other assignment operators &=, |=, ^=, <<=, and >>= bitwise assignment operators.

    Example of +=

    sum=2
    let "sum=sum+=2"
    echo $sum
    
    sum=2
    let "sum += 2"
    echo $sum
    

    Running the script gives the result:

    $ ./assignments.sh
    4
    

Log in to reply
 

© Lightnetics 2024