Linux Command Reference ยท Bash Syntax
Bash arithmetic expansion
Reference for Bash arithmetic expansion $(( )): integer operators, precedence, bases, bit operations, the (( )) command, assignment operators and overflow.
Arithmetic expansion evaluates an integer expression and replaces it with the result: $(( 3 + 4 )) becomes 7. Inside the double parentheses you can use variable names without a dollar sign, assign to them, and use most of the operators from C.
Bash only does integer arithmetic, using signed 64-bit numbers on modern systems. For decimals you need an external tool such as bc or awk. The related (( expression )) form is a command that succeeds when the result is non-zero, which is convenient in if and while.
echo $(( 7 * 6 )) # 42 (( total += 5 )) # no output, changes the variable if (( total > 3 )); then echo big; fi
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
$(( expression )) # expansion: yields the value (( expression )) # command: status 0 if value is non-zero let "expression" # older builtin form
| Operators (highest precedence first) | Description |
|---|---|
id++ id-- ++id --id |
Post and pre increment or decrement. |
+ - ! ~ |
Unary plus, minus, logical not, bitwise not. |
** |
Exponentiation (right associative). |
* / % |
Multiply, integer divide, remainder. |
+ - |
Add, subtract. |
<< >> |
Bit shifts. |
<= >= < > == != |
Comparison, giving 1 or 0. |
& ^ | |
Bitwise AND, XOR, OR. |
&& || |
Logical AND, OR (short-circuit). |
? : |
Conditional. |
= += -= *= /= %= <<= >>= &= ^= |= |
Assignment. |
, |
Sequence. |
Returns: a decimal integer. The (( )) command returns 0 for a non-zero result and 1 for zero, the reverse of the value.
Errors: division by zero and invalid tokens print an error and the expression fails (a non-interactive shell stops the current command). Overflow wraps silently.
Usage notes
- Variables do not need
$inside, and an unset or empty variable counts as 0. A variable holding text such asabcis evaluated recursively as a variable name, which can surprise you. - Leading zeros mean octal.
08is an error. Force decimal with10#$n, which is important for things like minutes read fromdate. - Truncation, not rounding.
7/2is3, and the remainder takes the sign of the dividend (-17 % 5is-2). - Integer only.
$(( 1.5 + 1 ))fails. For fractions usebc(bc) orawk. - Use
(( ))for conditions and[ ]or[[ ]]for strings. Mixing them up ([ $a > $b ]is a string comparison and a redirect) is a classic bug. - Exit status trap:
(( count++ ))returns status 1 when the old value was 0, which kills scripts running underset -e. Usecount=$((count + 1))there. - Spaces are optional but make code readable. A
$before variables is allowed and is required for positional parameters like$1. - Other tools:
expr(expr) does this in POSIX shells;letis a Bash builtin (let).
Related
let,expr,bc, Test operators, Strict mode- Lessons: Arithmetic, Conditionals