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


Arithmetic

The shell has an arithmetic unit capable of quite a range of arithmetic operation on integer variables. The basic function of this unit is to convert an arbitrary string into an integer by interpreting it as an arithmetic expression. This expression may contain variable names as well as literal integers and operators. As well as producing an result integer, the evaluation may, as a side effect, alter the value of some variable(s).

Whenever a variable is referred to in an arithmetic expression, that variable is first converted into an integer variable (if necessary). This may involve a recursive call to the arithmetic unit. Also, if the variable referred to is a component of an array, the arithmetic unit will be called recusively to evaluate the index.

An arithmetic expression can contain integer literals, variable references, operators and parentheses. Spaces may appear freely between any of these tokens, and are ignored.

Integer Literals
Integers may be specified either in the normal base 10, or in any other base by specifying the base (written in base 10) followed by a hash (#) and then the digits of the number. Hence the following are equivalent.
54
13#42
2#110110
11#4A
either upper or lower case can be used for digits greater than ten, with the traditional a=ten, b=eleven etc interpretation. This means that bases higher than 36 are not available.
Variable References
A variable may be refered to simply by giving its name. As mentioned above, the variable will be converted to an integer, if it isn't already. Elements of arrays can be accessed in the same way as anywhere else, by suffixing the name with an index enclosed in brackets. e.g.
array[4+3]
Note that range indicies are not permitted, or meaningful, in this context.
Operators
The operators available are essentially the same set as used in the C programming language. They are, in order of decreasing precedence:
Assignment Operators:
	=	+=	-=	/=	*=	%=	&=	|=
These require a variable reference on the left hand side, and assign to that variable the value from the right hand side, possibly combined with the original value of the variable by the appropriate operator.
Bitwise Disjunction Operators:
! | These are the exclusive and inclusive or operators and operate bitwise on the two values.
Bitwise Conjunction Operator:
& This produces a bitwise and of the two values.
Relational Operators:
== <= >= < > != These compare the two values and return 1 if the relationship holds, and 0 if it doesn't.
Addition Operators:
+ --- Integer addition and subtraction.
Multiplicative Operators:
* / % Integer multiplication, division and remainder operators.
Unary Prefix Operators:
! ++ -- - + The operators are, respectively, bitwise inversion, pre-increment, pre-decrement, negation and absolute value. The pre-increment and pre-decrement must precede a varaible reference.
Unary Suffix Operators:
++ -- These are the post-increment and post-decrement operators and must apply to variable references.
Parentheses
Parentheses over-ride operator precedence and may be nested arbitrarily deeply.


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