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

In Bash, an integer is a numeric value represented internally as a signed integer (typically 64-bit on modern systems) but stored and manipulated by default as a character string. Because Bash is weakly typed, a variable acts as an integer only when evaluated within an arithmetic context or when explicitly assigned an integer attribute.

## Explicit Integer Declaration

Variables can be strictly typed as integers using the `declare` or `typeset` built-ins with the `-i` option. Once the integer attribute is set, any subsequent assignment to that variable automatically forces an arithmetic evaluation.

When a non-numeric string is assigned to an integer-typed variable, Bash performs recursive evaluation, treating the string as a variable name. It does not automatically default to zero unless the referenced variable is unset or empty.

```bash theme={"dark"}
declare -i my_int
my_int=5+5      # Evaluates to 10, not the literal string "5+5"


# Recursive evaluation of strings in arithmetic contexts
string=42
my_int="string" # Evaluates to 42, as "string" is evaluated as a variable name
my_int="unset"  # Evaluates to 0, as the variable "unset" is empty/undefined
```

## Arithmetic Evaluation Contexts

Bash processes strings as integers when they are placed inside specific arithmetic constructs. Variables referenced inside these contexts do not require the `$` prefix for evaluation.

* **Arithmetic Expansion (`$((...))`):** Evaluates the expression and replaces the construct with the resulting integer.
* **Arithmetic Command (`((...))`):** Evaluates the expression and returns an exit status (0 if the mathematical result is non-zero, 1 if the result is zero).
* **The `let` Built-in:** Evaluates arithmetic expressions passed as string arguments.

```bash theme={"dark"}

# Arithmetic expansion
result=$(( 10 * 2 ))


# Arithmetic command
(( result++ ))


# let built-in
let "result = result / 2"
```

## Radix (Base) Representation

Bash integers support arbitrary bases from 2 to 64. The standard syntax for defining a base is `base#number`. Bash also recognizes standard C-style prefixes for octal and hexadecimal integers. To evaluate these representations as integers upon assignment, they must be wrapped in an arithmetic expansion or assigned to a strictly typed integer variable; otherwise, they are treated as literal strings.

```bash theme={"dark"}

# Using arithmetic expansion for base evaluation
bin_val=$(( 2#1010 ))      # Binary (Base 2) representation of 10
hex_val=$(( 16#FF ))       # Hexadecimal (Base 16) representation of 255
base64_val=$(( 64#@ ))     # Base 64 representation


# C-style prefixes
oct_val=$(( 0755 ))        # Leading '0' denotes Octal (Base 8)
hex_val_c=$(( 0xFF ))      # Leading '0x' or '0X' denotes Hexadecimal


# Using explicit integer declaration
declare -i typed_bin
typed_bin=2#1010           # Evaluates to 10 due to the integer attribute
```

## Technical Characteristics and Limitations

* **Precision:** Bash integers are fixed-width signed integers. On 64-bit systems, the range is `-9223372036854775808` to `9223372036854775807`.
* **Overflow:** Bash does not throw exceptions on integer overflow or underflow. It silently wraps around the maximum or minimum values.
* **Floating-Point:** Bash has no native support for floating-point arithmetic. Division operations strictly perform integer division, truncating any fractional remainder toward zero.

```bash theme={"dark"}

# Integer division truncation
echo $(( 10 / 3 ))  # Outputs 3


# Overflow wraparound (on 64-bit systems)
echo $(( 9223372036854775807 + 1 )) # Outputs -9223372036854775808
```

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