This is an old revision of the document!


The let builtin

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.

  • 0 (TRUE) when arg evaluated to not 0 (arithmetic "true")
  • 1 (FALSE) when arg evaluated to 0 (arithmetic "false")

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 enables the effect that the variable effectively becomes "private" to the arithmetic expression unless the variable is also set or modified by the expression. The value set in the environment is a string even if the variable has previously been given the -i attribute, and even if modified by the expression retains all the same attributes afterwards. These examples demonstrate these, and also that other built-ins do it too, though it would probably be a very unusual corner-case to use this with anything other than let.

 ~ $ ( 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"

 ~ $ ( declare -i y; y=1+1 z='z[$(echo $y >&2)]=0' let z,y+=x=y; declare -p x y )
1+1
declare -- x="2"
declare -i y="4"

 ~ $ ( y=1+1 declare -a 'x=( [x=y]=test )'; declare -p x y )
declare -a x='([0]="2" [2]="test")'
bash: declare: y: not found

 ~ $ ( y=1+1 declare -a 'x=( [x=y++]=test )'; declare -p x y )
declare -a x='([0]="2" [2]="test")'
declare -- y="3"

 ~ $ ( y=1+1 printf -v 'x[x=y]' %s test; declare -p x y )
declare -a x='([0]="2" [2]="test")'
bash: declare: y: not found

 ~ $ ( y=1+1 printf -v 'x[x=y++]' %s test; declare -p x y )
declare -a x='([0]="2" [2]="test")'
declare -- y="3"

Portability considerations

  • the let command is not specified by POSIX®. The standard alternative is:
    [ "$(( <EXPRESSION> ))" -ne 0 ]
  • expr(1) is a command one is likely to come across sooner or later. While it is more "standard" than let, the above should always be preferred. Both arithmetic expansions and the [ test operator are specified by POSIX® and satisfy almost all of expr's use-cases. Unlike let, expr cannot assign directly to bash variables but instead returns a result on stdout. expr takes each operator it recognizes as a separate word and then concatenates them into a single expression that's evaluated according to it's own rules (which differ from shell arithmetic). let parses each word it recieves on its own and evaluates it as an expression without generating any output other than a return code.

See also

Discussion

joshua toyota, 2011/01/13 09:33

I believe the internal links to "arithmetic expansion" should be directed here:

http://wiki.bash-hackers.org/syntax/expansion/arith

Instead of:

http://wiki.bash-hackers.org/syntax/arith_expr

Just an fyi...

Jan Schampera, 2011/01/13 17:40

Of course, my bad...

Thanks

Enter your comment. Wiki syntax is allowed: