Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Arithmetic expansion is a shell parsing mechanism that evaluates a mathematical expression and replaces the expression with its calculated integer result. It utilizes the shell’s internal arithmetic evaluator, treating the enclosed contents as a C-language expression.

Syntax

The primary syntax for arithmetic expansion uses double parentheses preceded by a dollar sign:
$(( expression ))
When the result does not need to be substituted into a command or assigned to a variable, the evaluation command syntax omits the dollar sign:
(( expression ))

Evaluation Mechanics

  1. Integer Arithmetic: Bash arithmetic operates exclusively on fixed-width integers (typically 64-bit, depending on the architecture). Floating-point numbers are not supported and will trigger a syntax error.
  2. Expansion Order: The shell performs parameter expansion, command substitution, and quote removal on the expression before evaluating it arithmetically.
  3. Variable Dereferencing: Within the (( )) or $(( )) context, the $ prefix is optional for standard variables. Variables referenced without a $ are evaluated as arithmetic expressions, not strictly cast to integers. For example, if var="5+5", $((var)) evaluates the underlying expression and returns 10.
  4. Null and Unset Variables: Any unset variable or variable containing a null value evaluates to 0 within the arithmetic context.
  5. String Evaluation: If a variable contains a string, Bash attempts to evaluate it recursively as an arithmetic expression or variable name. If the string does not form a valid arithmetic expression or valid variable name (e.g., var="hello world" or var="1/0"), Bash will throw a syntax error or math error. It only evaluates to 0 if the string resolves to a valid identifier (variable name) that is unset or null.

Supported Operators

Bash supports standard C-style operators, evaluated with standard C precedence rules.
  • Post/Pre-increment and decrement: id++, id--, ++id, --id
  • Unary: -, +
  • Logical/Bitwise Negation: !, ~
  • Exponentiation: **
  • Multiplication, Division, Remainder: *, /, %
  • Addition, Subtraction: +, -
  • Bitwise Shifts: <<, >>
  • Relational: <=, >=, <, >
  • Equality: ==, !=
  • Bitwise AND, XOR, OR: &, ^, |
  • Logical AND, OR: &&, ||
  • Ternary Operator: expr ? expr : expr
  • Assignment: =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
  • Comma (Sequential Evaluation): expr1 , expr2 (evaluates both, returns the value of expr2)

Radix and Base Representation

By default, numbers are evaluated as base 10. Bash supports arbitrary bases from 2 to 64 using the base#number notation.
$(( 2#1010 ))    # Binary
$(( 8#77 ))      # Octal
$(( 16#FF ))     # Hexadecimal
$(( 64#@a ))     # Base 64
Additionally, Bash recognizes standard C-style prefixes for specific bases:
  • 0x or 0X: Hexadecimal (base 16)
  • 0: Octal (base 8)
Octal Padding Caveat: Because a leading 0 forces octal evaluation, zero-padded numbers containing the digits 8 or 9 (e.g., 08 or 09) are invalid in base 8 and will trigger a “value too great for base” syntax error. To safely perform arithmetic on zero-padded decimal strings, explicitly force base 10 evaluation using the 10# prefix:
$(( 10#08 ))     # Forces base 10, evaluates to 8

Exit Status

When using the standalone arithmetic evaluation command (( expression )), the shell’s exit status ($?) is determined by the boolean truth of the evaluated expression:
  • If the expression evaluates to a non-zero value, the exit status is 0 (True/Success).
  • If the expression evaluates to zero, the exit status is 1 (False/Failure).
When using arithmetic expansion $(( expression )) as part of a larger command, the exit status is determined by the execution of the resulting command, not the arithmetic evaluation itself.
Master Bash with Deep Grasping Methodology!Learn More