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.

The following two lines are equivalent: the second form avoids quoting and enhances readability. These two forms are extensions to the POSIX standard.
let "expression"
((expression))
The POSIX version of this command is as follows:
$((expression))

Expressions consist of named variables, numeric constants, and operators. Characters in the names of named variables must come from thePOSIX portable character set.

See Arithmetic substitution.

Examples

Examples of the three forms of the let command are as follows:

  1. The example
    let a=7
    echo $a
    produces:
    7
  2. The example
    echo $((a=7*9))
    produces:
    63
  3. The example
    ((a=3*4))
    echo $a
    produces:
    12

Usage notes

let is a built-in shell command.

Localization

let uses the following localization environment variables:
  • 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