LinuxCommandLibrary

((

TLDR

View documentation for the original command

$ tldr let
copy

SYNOPSIS

(( [n=]expression ))

PARAMETERS

expression
    Arithmetic expression to evaluate, using operators like + - * / % ++ -- & | ^ << >>; supports assignments (var=val) and conditionals

DESCRIPTION

The (( )) construct in Bash is a powerful arithmetic evaluation operator used for performing integer arithmetic operations directly within shell scripts. It treats variables as integers, supports a wide range of operators (arithmetic, bitwise, logical), and provides assignment capabilities.

Unlike traditional command substitution, (( )) does not produce output; instead, it evaluates the expression and sets the exit status: 0 if the result is non-zero, 1 if zero or null. Variables referenced inside are expanded without $, and undefined variables default to 0.

It excels in conditional tests, loops, and calculations, offering speed advantages over external tools like expr or bc. For example, (( count++ )) increments count by 1, and (( a = b * 2 + c )) assigns the computed value to a. Supports C-style operators like ++, --, &, |, &&, ||, and comparisons (==, !=, <).

Ideal for scripting efficiency, it avoids forking processes, making scripts faster and more portable within Bash environments.

CAVEATS

Integer-only arithmetic (64-bit signed); no floating-point support. Division by zero causes runtime error. Limited to Bash/ksh/zsh; not POSIX sh compliant.

EXIT STATUS

0 if expression evaluates to non-zero; 1 if zero or empty. Use in if (( expr )); then or while (( expr )); do.

EXAMPLES

(( x = 5 + 3 ))
(( y++ ))
if (( a > b )); then echo 'greater'; fi

HISTORY

Introduced in Bash 2.05b (1999) inspired by ksh93; enhanced in Bash 3.1 with more operators. Widely used in modern scripting for performance.

SEE ALSO

let(1), expr(1), bc(1), awk(1)

Copied to clipboard