> ## 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.

# Bash Arithmetic Command

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.

```bash theme={"dark"}
(( 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.

```bash theme={"dark"}
$(( 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).

```bash theme={"dark"}

# 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).

```bash theme={"dark"}

# 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).

```bash theme={"dark"}
(( 5 - 5 ))  # Result is 0. Exit status ($?) is 1.
(( 5 + 5 ))  # Result is 10. Exit status ($?) is 0.
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Bash Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
