Table of Contents

The let builtin command

Synopsis

let arg [arg ...]

Description

The let builtin command evaluates each supplied word from left to right as an arithmetic expression and returns an exit code according to the truth value of the rightmost expression.

For this return code mapping, please see this section. They work in the same way as ((.

Examples

let is very similar to (( - the only difference being let is a builtin (simple command), and (( is a compound command. The arguments to let are therefore subject to all the same expansions and substitutions as any other simple command - requiring proper quoting and escaping - whereas the contents of (( aren't subject to word-splitting or pathname expansion (almost never desirable for arithmetic). For this reason, the arithmetic compound command should generally be preferred over let.

$ let 'b = a' "(a += 3) + $((a = 1)), b++"
$ echo "$a - $b - $?"
4 - 2 - 0

Is equivalent to the arithmetic evaluation compound command:

$ (( b = a, (a += 3) + $((a = 1)), b++ ))
$ echo "$a - $b - $?"
4 - 2 - 0

Remember that inside arithmetic evaluation contexts, all other expansions are processed as usual (from left-to-right), and the resulting text is evaluated as an arithmetic expression. Arithmetic already has a way to control precedence using parentheses, so it's very rare to need to nest arithmetic expansions within one another. It's used above only to illustrate how this precedence works.

Unlike ((, being a simple command let has its own environment. In Bash, built-ins that can set variables process any arithmetic under their own environment, which makes the variable effectively "local" to the builtin unless the variable is also set or modified by the builtin. This differs in other shells, such as ksh93, where environment assignments to regular builtins are always local even if the variable is modified by the builtin.

 ~ $ ( y=1+1 let x=y; declare -p x y )
declare -- x="2"
bash: declare: y: not found

 ~ $ ( y=1+1 let x=y++; declare -p x y )
declare -- x="2"
declare -- y="3"

This can be useful in certain situations where a temporary variable is needed.

Portability considerations

See also