Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

let - integer arithmetic

&pagelevel(4)&pagelevel

The built-in shell command let provides integer arithmetic. Calculations are performed using long arithmetic. Constants are represented in the form [base#]n where base is an integer between 2 and 36 which specifies the base and n is a number accompanying this base. If base is not specified, calculations are performed in base ten.


Syntax


Format 1:let argument ...
Format 2:((argument)) ...



Format 1:let argument ...


argument

Each argument is an arithmetic expression. The results obtained on evaluation are output.


Format 2:((argument)) ...


Since many of the arithmetic operators must be quoted for the POSIX shell, an alternative form of the let command is provided. For any command which begins with a double left parenthesis ((, all the characters until a matching double right parenthesis )) are treated as a quoted expression.

Thus ((a=a+b)) is equivalent to let "a=a+b".

Arithmetic evaluation

An arithmetic expression uses much the same syntax, precedence, and associativity as the C language. All the integral operators, other than ++, --, ?:, and the comma are supported. Variables can be referenced by name within an arithmetic expression without using the parameter substitution syntax (the $ character). When a variable is referenced, its value is evaluated as an arithmetic expression.

An internal integer representation of a variable can be specified as an attribute with the -i option of the built-in typeset command. Arithmetic evaluation is performed on the value of each assignment to a variable with the -i attribute. If you do not specify an arithmetic base, the first assignment to the variable determines the arithmetic base. This base is used when parameter substitution is performed.

Exit status

0if the value of the last expression was not equal to 0
1otherwise.

Error

sh:<expr>: bad number

Incorrect expression

Example

The following example illustrates a simple arithmetic operation. Both let notations are used.

$ let var=10

$ echo $var

10

$ ((var=var-3))

$ echo $var

7