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.

The Bash arithmetic command (( )) is a shell built-in compound command used to evaluate mathematical expressions, perform integer arithmetic, and execute C-style variable manipulations directly within the shell environment. Closely related is Arithmetic Expansion $(( )), which evaluates expressions and substitutes the resulting string back into the command line. Both mechanisms operate without spawning external processes, making them highly efficient for internal shell calculations.

Syntax Contexts

Arithmetic evaluation in Bash manifests in two distinct syntactic forms, which serve different parsing roles: 1. The Arithmetic Command (Compound Command) Evaluates an expression as a standalone command. It does not yield a string substitution but modifies variables in place and returns a shell exit status based on the mathematical result. It is synonymous with the let built-in.
(( expression ))
2. Arithmetic Expansion Evaluates the expression and substitutes the resulting integer string directly into the command line before the command is executed. Because it is an expansion, it cannot act as a standalone command and must be passed as an argument or assigned to a variable.
$(( expression ))

Technical Mechanics

  • Integer Limitation: The arithmetic engine strictly processes fixed-width integers (typically 64-bit signed integers on modern systems). It does not support floating-point arithmetic. Division truncates towards zero.
  • Implicit Dereferencing: Within the (( )) or $(( )) constructs, shell variables are automatically dereferenced. The $ prefix is optional for standard variables, though it remains mandatory for positional parameters (e.g., $1) and special parameters (e.g., $#).
  • Whitespace: The parser ignores spaces and tabs between operators and operands, allowing for flexible formatting.
  • Undeclared Variables: Under default shell options, variables that are null or unset evaluate to 0 without throwing an error. However, if the nounset option is enabled (set -u), referencing an unset variable inside an arithmetic context will throw an “unbound variable” error.

Supported Operators

Bash arithmetic supports a comprehensive set of C-style operators, evaluated in standard order of precedence:
  • Arithmetic: + (addition), - (subtraction), * (multiplication), / (integer division), % (modulo), ** (exponentiation).
  • Pre/Post Increment & Decrement: ++var, --var, var++, var--.
  • Assignment: =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=.
  • Bitwise: ~ (NOT), << (left shift), >> (right shift), & (AND), ^ (XOR), | (OR).
  • Logical: ! (NOT), && (AND), || (OR).
  • Relational: <, <=, >, >=, ==, !=.
  • Ternary: condition ? expr1 : expr2.
  • Comma: , (evaluates a sequence of expressions and returns the value of the last expression).

# Syntax visualization of operators
(( var = a ** 3 ))             # Exponentiation and assignment
(( var++ ))                    # Post-increment
(( result = (a > b) ? a : b )) # Ternary operation
(( a=1, b=2, c=a+b ))          # Comma operator evaluating multiple expressions

Base and Radix Representation

By default, numbers are treated as base 10. The arithmetic engine supports alternative numeral systems using specific prefixes or the base#number notation:
  • Octal: Prefix with 0 (e.g., 077).
  • Hexadecimal: Prefix with 0x or 0X (e.g., 0xFF).
  • Arbitrary Base: Use base#number where base is between 2 and 64 (e.g., 2#1010 for binary, 16#FF for hexadecimal).

# Syntax visualization of base representation using Arithmetic Expansion
echo $(( 2#1010 | 2#0101 ))    # Bitwise OR with binary literals, passed to echo
result=$(( 16#A + 012 ))       # Hexadecimal 10 plus Octal 10, assigned to a variable

Exit Status Behavior

When using the Arithmetic Command (( expression )), the shell exit status ($?) is determined by the mathematical result of the expression, which is the inverse of standard C logic:
  • If the expression evaluates to a non-zero value, the exit status is 0 (Success/True).
  • If the expression evaluates to zero, the exit status is 1 (Failure/False).
(( 5 - 5 ))  # Result is 0. Exit status ($?) is 1.
(( 5 + 5 ))  # Result is 10. Exit status ($?) is 0.
Master Bash with Deep Grasping Methodology!Learn More