LinuxCommandLibrary

let

Perform arithmetic calculations within shell scripts

TLDR

Evaluate a simple arithmetic expression

$ let "[result = a + b]"
copy

Use post-increment and assignment in an expression
$ let "[x++]"
copy

Use conditional operator in an expression
$ let "[result = (x > 10) ? x : 0]"
copy

Display help
$ let --help
copy

SYNOPSIS

let EXPRESSION [EXPRESSION ...]

PARAMETERS

EXPRESSION
    One or more arithmetic expressions to be evaluated. These expressions follow standard C-language arithmetic precedence and can include variables, integers, and operators (e.g., +, -, *, /, %, ==, !=, <, >). Variables within the expression are automatically dereferenced.

DESCRIPTION

let is a shell built-in command that evaluates one or more arithmetic expressions. It's primarily used for integer arithmetic. Each EXPRESSION is evaluated as if it were part of a ((...)) construct. Variables within the expression do not need to be prefixed with $ as they are dereferenced automatically.

The command returns an exit status of 0 if the last EXPRESSION evaluates to a non-zero value, and 1 if it evaluates to zero. This makes let useful in conditional statements. It's often used for incrementing/decrementing loop counters or performing simple calculations directly within shell scripts. While powerful, modern shell scripting often prefers the ((...)) construct for arithmetic evaluation due to its cleaner syntax.

CAVEATS

let performs only integer arithmetic; it does not support floating-point numbers.

It is a shell built-in command, meaning its behavior can vary slightly between different shell implementations (e.g., Bash, Zsh) but is largely standardized.

Return status: The exit status is 0 if the result of the last EXPRESSION is non-zero, and 1 if it is zero. This is the opposite of typical true/false evaluation in arithmetic, where 0 is false and non-zero is true.

OPERATORS

let supports common C-style arithmetic and bitwise operators, including +, -, *, /, % (modulo), ** (exponentiation, Bash 4+), <<, >>, &, |, ^, ! (logical NOT), && (logical AND), || (logical OR), ? : (ternary operator), and assignment operators like +=, -=.

RETURN STATUS EXAMPLE

The command let "a=10" && echo "Non-zero" || echo "Zero" will output "Non-zero".

The command let "a=0" && echo "Non-zero" || echo "Zero" will output "Zero", because let returns 1 when the result of the last expression is zero.

HISTORY

The let command has been a standard built-in in many shells, including Bash, for a long time, often specified by POSIX. It provided a way to perform arithmetic calculations directly within the shell environment without invoking external utilities like expr. While still fully functional, its usage has somewhat declined in favor of the more modern and often cleaner ((...)) arithmetic expansion/command in shells like Bash, which offers similar functionality and syntax.

SEE ALSO

expr(1), bc(1), test(1), ((...)) - Bash arithmetic expansion, declare -i

Copied to clipboard