Arithmetic expansions

Arithmetic expansion provides a mechanism for evaluating an arithmetic expression and substituting its value. The format for arithmetic expansion is:

$((expression))

The expression is treated as if it were in double quotation marks, except that a double quotation mark inside expression is not treated specially. The shell expands all tokens in expression for parameter expansion, command substitution, and quote removal. qsh treats the result as an arithmetic expression and substitutes the value of the expression.

Arithmetic expressions

An arithmetic expression can be specified in the following situations:

qsh performs either integer or floating point arithmetic based on the setting of the float option. When the float option is set on, qsh performs floating point arithmetic.

An integer number has the format [base#]number where:

A floating point number has the format [+|-] number[.number] [exponent] where:

Arithmetic expressions use the following ANSI C language operators and precedence.

(expression)
Parenthesis overrides precedence rules
Unary operators
+expression Unary +
-expression Unary -
~expression Bitwise negation
!expression Logical negation
Multiplicative operators
expression * expression Multiplication
expression / expression Division
expression % expression Remainder
Additive operators
expression + expression Addition
expression - expression Subtraction
Bitwise shift operators
expression << expression Left shift the first expression by the number of bits given in the second expression
expression >> expression Right shift the first expression by the number of bits given in the second expression
Relational operators
expression < expression Less than
expression <= expression Less than or equal to
expression > expression Greater than
expression >= expression Greater than or equal to
Bitwise AND operator
expression & expression Bitwise and where the result contains a 1 in each bit position where there is a 1 in both expressions and a 0 in all other bit positions.
Bitwise Exclusive OR operator
expression ^ expression Bitwise exclusive or where the result contains a 1 in each bit position where there is a 1 in only one of the expressions and a 0 in all other bit positions.
Bitwise OR operator
expression | expression Bitwise or where the result contains a 1 in each bit position where there is a 1 in either expression and a 0 in all other bit positions.
Logical AND operator
expression && expression Logical and where the result is true if both expressions are true
Logical OR operator
expression || expression Logical or where the result is true if one of the expressions is true
Conditional operator
expression ? expression : expression Conditional operator where when the first expression is true, the second expression is evaluated. Otherwise the third expression is evaluated.
Assignment operators
expression = expression Simple assignment
expression *= expression Assign and multiply
expression /= expression Assign and divide
expression %= expression Assign and remainder
expression += expression Assign and add
expression -= expression Assign and subtract
expression <<= expression Assign and shift left
expression >>= expression Assign and shift right
expression &= expression Assign and bitwise AND
expression ^= expression Assign and bitwise exclusive OR
expression |= expression Assign and bitwise OR
Note: When using floating point arithmetic the remainder, left shift, right shift, bitwise AND, bitwise exclusive OR, and bitwise OR operators are not supported.

Examples

  1. Add two decimal numbers:
    echo $((2+2))
    
  2. Add two hexadecimal numbers:
    echo $((16#A + 16#20))
    
  3. Increment the variable index by one:
    let index+=1
    
  4. Evaluate a complex expression:
    echo $((5+9-2*3/2))
    
  5. Add two floating point numbers:
    set -F
    echo $((5.75+9.157))
    set +F