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

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:

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

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

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

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

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