Arithmetic expansion provides a powerful tool for performing arithmetic operations in scripts. Translating a string into a numerical expression is relatively straightforward using backticks, double parentheses, or let.
z=`expr $z + 3` # The 'expr' command performs the expansion. |
The use of backticks in arithmetic expansion has been superseded by double parentheses $((...)) or the very convenient let construction.
z=$(($z+3)) # $((EXPRESSION)) is arithmetic expansion. # Not to be confused with #+ command substitution. let z=z+3 let "z += 3" # Quotes permit the use of spaces and special operators. # The 'let' operator actually performs arithmetic evaluation, #+ rather than expansion. |
Examples of arithmetic expansion in scripts: