7.7. ArithmeticThe let command allows you to perform arithmetic (Figure 7-15).
Here are some examples: $ let x = 2 + 2 ...expression contains spaces. ksh: =: syntax error ...no spaces or tabs allowed! $ let x=2+2 ...OK. $ echo $x 4 7.7.1. Preventing Metacharacter InterpretationUnfortunately, the shell interprets several of the standard operators, such as <, >, and *, as metacharacters, so they must be quoted or preceded by a backslash ( \ ) to inhibit their special meaning. To avoid this inconvenience, there is an equivalent form of let that automatically treats all of the tokens as if they were surrounded by double quotes, and allows you to use spaces around tokens. The token sequence: (( list )) is equivalent to: let " list " Note that double quotes do not prevent the expansion of variables. I personally always use the ((..)) syntax instead of let. Here's an example: $ (( x = 4 )) ...spaces are OK. $ (( y = x * 4 )) $ echo $y 16 $ _ 7.7.2. Testing ValuesArithmetic values may be used by decision-making control structures, such as an if statement: $ (( x = 4 )) ...assign x to 4. $ if (( x > 0 )) ...OK to use in a control structure. > then > echo x is positive > fi x is positive ...output from control structure. $ _ For simple arithmetic tests, I recommend using ((..)) instead of test expressions. |