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. |
  1. echo $((0xff))
  2. 255
  3. echo $((037))
  4. 255
  5. $ echo $((2#11111111))
  6. 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 $p
1. p = p + 1
parameter−− Variable post-decrement.
echo $(( p-- )) Equivalent to:
1. echo $p
1. p = p - 1
++parameter Variable pre-increment.
echo $(( ++p )) Equivalent to:
1. p = p + 1
1. echo $p
—parameter Variable pre-decrement.
echo $(( --p )) Equivalent to:
1. p = p - 1
1. 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.
  1. $ a=0
  2. $ ((a<1?++a:--a))
  3. $ echo $a
  4. 1
  5. $ ((a<1?++a:--a))
  6. $ echo $a
  7. 0
  1. $ a=0
  2. $ ((a<1?a+=1:a-=1)) # error
  3. bash: ((: a<1?a+=1:a-=1: attempted assignment to non-variable (error token is "-=1")
  4. $ ((a<1?(a+=1):(a-=1))) # This problem can be mitigated by surrounding the assignment expression with parentheses.