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.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.
Syntax
The primary syntax for arithmetic expansion uses double parentheses preceded by a dollar sign:Evaluation Mechanics
- 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.
- Expansion Order: The shell performs parameter expansion, command substitution, and quote removal on the expression before evaluating it arithmetically.
- 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, ifvar="5+5",$((var))evaluates the underlying expression and returns10. - Null and Unset Variables: Any unset variable or variable containing a null value evaluates to
0within the arithmetic context. - 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"orvar="1/0"), Bash will throw a syntax error or math error. It only evaluates to0if 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 ofexpr2)
Radix and Base Representation
By default, numbers are evaluated as base 10. Bash supports arbitrary bases from 2 to 64 using thebase#number notation.
0xor0X: Hexadecimal (base 16)0: Octal (base 8)
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:
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).
$(( 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





