let — Evaluate an arithmetic expression
Format
- let expression …
- ((expression))
Description
let evaluates each arithmetic expression from left to right, with normal algebraic precedence (multiplication before addition, for example). let uses long integer arithmetic with no checks for overflow. No output is generated; the exit status is 0 if the last expression argument has a nonzero value, and 1 otherwise.
let "expression"
((expression))
$((expression))
Expressions consist of named variables, numeric constants, and operators. Characters in the names of named variables must come from thePOSIX portable character set.
Examples
Examples of the three forms of the let command are as follows:
- The example
produces:let a=7 echo $a
7
- The example
produces:echo $((a=7*9))
63
- The example
produces:((a=3*4)) echo $a
12
Usage notes
let is a built-in shell command.
Localization
- LANG
- LC_ALL
- LC_MESSAGES
- NLSPATH
See Localization for more information.
Exit values
- 0
- The last argument evaluated to a nonzero value
- 1
- The last argument evaluated to a zero value, or the expression contained a syntax error or tried to divide by zero
Portability
POSIX.2. let and ((expression)) are extensions to the POSIX.2 standard. The POSIX.2 portable facility for arithmetic expression evaluation is $((expression)). See Arithmetic substitution for more information.
The (( )) syntax only works if the set –o korn option is in effect.
Related information
expr, sh, test