This is an old revision of the document!

The let builtin


let arg [arg ...]


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 ((.


$ 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

The latter is almost always preferred. No wordsplitting or pathname expansion occurs within the arithmetic command. However, all the usual expansions and argument passing rules which apply to ordinary simple commands also apply to let. Be careful about quoting and escaping. A rough analogy: [ : [[ :: let : ((, except that let has no standalone command and is non-standard.

Portability considerations

  • the let command is not specified by POSIX®. The standard equivalent 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.
  • the quotes around the arithmetic expansion are only necessary with Bash and AT&T versions of ksh, other standard shells such as ash, pdksh or zsh derivatives don't have that bug/misfeature.

See also


joshua toyota, 2011/01/13 09:33

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

Instead of:

Just an fyi...

Jan Schampera, 2011/01/13 17:40

Of course, my bad...


Enter your comment. Wiki syntax is allowed: