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:
- in an arithmetic expansion
- for each argument of the let utility
- for the argument of the shift
utility
- for the operands of the arithmetic formats of the printf utility
- for the operands to the arithmetic comparison operators of the
test utility
- for the argument of the
ulimit utility
- in the "Substring Starting at Offset" parameter expansion
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:
- base is a decimal integer between 2 and 36 that
specifies the arithmetic base. The default is base 10.
- number is a non-negative number. For a base greater
than 10, numbers greater than 9 or represented using a letter of
the alphabet. For example, when using base 16, the decimal number
10 is represented using A.
A floating point number has the format [+|-]
number[.number] [exponent] where:
- number is a non-negative decimal number.
- exponent is E or e followed by + or - and a
non-negative decimal number.
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
- Add two decimal numbers:
echo $((2+2))
- Add two hexadecimal numbers:
echo $((16#A + 16#20))
- Increment the variable index by one:
let index+=1
- Evaluate a complex expression:
echo $((5+9-2*3/2))
- Add two floating point numbers:
set -F
echo $((5.75+9.157))
set +F