The Bash arithmetic commandDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
(( )) 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 thelet built-in.
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
0without throwing an error. However, if thenounsetoption 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).
Base and Radix Representation
By default, numbers are treated as base 10. The arithmetic engine supports alternative numeral systems using specific prefixes or thebase#number notation:
- Octal: Prefix with
0(e.g.,077). - Hexadecimal: Prefix with
0xor0X(e.g.,0xFF). - Arbitrary Base: Use
base#numberwherebaseis between 2 and 64 (e.g.,2#1010for binary,16#FFfor hexadecimal).
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).
Master Bash with Deep Grasping Methodology!Learn More





