Basic Form
$((expression))
- where expression is a valid arithmetic expression.
Number Bases
| Notation | Description | | —- | —- | | number | By default, numbers without any notation are treated as decimal (base 10) integers. | | 0number | In arithmetic expressions, numbers with a leading zero are considered octal. | | 0xnumber | Hexadecimal notation. | | base#number | number is in base. |
echo $((0xff))255echo $((037))255$ echo $((2#11111111))255
Unary Operators
There are two unary operators, + and -, which are used to indicate whether
a number is positive or negative, respectively.
Arithmetic Operators
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Integer division |
| ** | Exponentiation |
| % | Modulo (remainder) |
Assignment Operators
| Notation | Description |
|---|---|
| parameter = value | Simple assignment. Assigns value to parameter. |
| parameter += value | Addition. Equivalent to parameter = parameter + value. |
| parameter -= value | Subtraction. Equivalent to parameter = parameter – value. |
| parameter *= value | Multiplication. Equivalent to parameter = parameter * value. |
| parameter /= value | Integer division. Equivalent to parameter = parameter / value. |
| parameter %= value | Modulo. Equivalent to parameter = parameter % value. |
| parameter++ | Variable post-increment. echo $(( p++ )) Equivalent to: 1. echo $p1. p = p + 1 |
| parameter−− | Variable post-decrement.echo $(( p-- )) Equivalent to: 1. echo $p1. p = p - 1 |
| ++parameter | Variable pre-increment. echo $(( ++p )) Equivalent to: 1. p = p + 11. echo $p |
| —parameter | Variable pre-decrement. echo $(( --p )) Equivalent to: 1. p = p - 11. echo $p |
| <<=, >>=, &=, |=, ^= | See Bit Operators |
Bit Operators
| Operator | Description |
|---|---|
| ~ | Bitwise negation. Negate all the bits in a number. |
| << | Left bitwise shift. Shift all the bits in a number to the left. |
| >> | Right bitwise shift. Shift all the bits in a number to the right. |
| & | Bitwise AND. Perform an AND operation on all the bits in two numbers. |
| | | Bitwise OR. Perform an OR operation on all the bits in two numbers. |
| ^ | Bitwise XOR. Perform an exclusive OR operation on all the bits in two numbers. |
Logic/Comparison Operators
| Operator | Description |
|---|---|
| <= | Less than or equal to. |
| >= | Greater than or equal to. |
| < | Less than. |
| > | Greater than. |
| == | Equal to. |
| != | Not equal to. |
| && | Logical AND. |
| || | Logical OR. |
| expr1?expr2:expr3 | Comparison (ternary) operator. If expression expr1 evaluates to be nonzero (arithmetic true), then expr2; else expr3. |
$ a=0$ ((a<1?++a:--a))$ echo $a1$ ((a<1?++a:--a))$ echo $a0
$ a=0$ ((a<1?a+=1:a-=1)) # errorbash: ((: a<1?a+=1:a-=1: attempted assignment to non-variable (error token is "-=1")$ ((a<1?(a+=1):(a-=1))) # This problem can be mitigated by surrounding the assignment expression with parentheses.
