Go to the first, previous, next, last section, table of contents.


Arithmetic Evaluation

The shell can perform integer arithmetic, either using the builtin let, or via a substitution of the form $((...)). The shell is usually compiled to use 8-byte precision where this is available, otherwise precision is 4 bytes. This can be tested, for example, by giving the command `print - $(( 12345678901 ))'; if the number appears unchanged, the precision is at least 8 bytes.

The let builtin command takes arithmetic expressions as arguments; each is evaluated separately. Since many of the arithmetic operators, as well as spaces, require quoting, an alternative form is provided: for any command which begins with a `((', all the characters until a matching `))' are treated as a quoted expression and arithmetic expansion performed as for an argument of let. More precisely, `((...))' is equivalent to `let "..."'. For example, the following statement

(( val = 2 + 1 ))

is equivalent to

let "val = 2 + 1"

both assigning the value 3 to the shell variable foo and returning a zero status.

Numbers can be in bases other than 10. A leading `0x' or `0X' denotes hexadecimal. Numbers may also be of the form `base#n', where base is a decimal number between two and thirty-six representing the arithmetic base and n is a number in that base (for example, `16#ff' is 255 in hexadecimal). The base# may also be omitted, in which case base 10 is used. For backwards compatibility the form `[base]n' is also accepted.

An arithmetic expression uses nearly the same syntax, precedence, and associativity of expressions in C. The following operators are supported (listed in decreasing order of precedence):

+ - ! ~ ++ --
unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement
<< >>
bitwise shift left, right
&
bitwise AND
^
bitwise XOR
|
bitwise OR
**
exponentiation
* / %
multiplication, division, modulus (remainder)
+ -
addition, subtraction
< > <= >=
comparison
== !=
equality and inequality
&&
logical AND
|| ^^
logical OR, XOR
? :
ternary operator
= += -= *= /= %= &= ^= |= <<= >>= &&= ||= ^^= **=
assignment
,
comma operator

The operators `&&', `||', `&&=', and `||=' are short-circuiting, and only one of the latter two expressions in a ternary operator is evaluated. Note the precedence of the bitwise AND, OR, and XOR operators.

An expression of the form `#\x' where x is any character sequence such as `a', `^A', or `\M-\C-x' gives the ascii value of this character and an expression of the form `#foo' gives the ascii value of the first character of the value of the parameter foo. Note that this is different from the expression `$#foo', a standard parameter substitution which gives the length of the parameter foo.

Named parameters and subscripted arrays can be referenced by name within an arithmetic expression without using the parameter expansion syntax. For example,

((val2 = val1 * 2))

assigns twice the value of $val1 to the parameter named val2.

An internal integer representation of a named parameter can be specified with the integer builtin. Arithmetic evaluation is performed on the value of each assignment to a named parameter declared integer in this manner.


Go to the first, previous, next, last section, table of contents.