> ## 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 Indexed Array

An indexed array in Bash is a zero-based, one-dimensional, sparse data structure that maps integer subscripts to string values. Because Bash arrays are inherently sparse, they do not require contiguous allocation, allowing elements to be assigned to arbitrary, non-sequential indices.

## Declaration

Arrays can be declared explicitly using the `declare` built-in with the `-a` option, or implicitly through assignment.

```bash theme={"dark"}

# Explicit declaration
declare -a my_array


# Implicit declaration via compound assignment
my_array=("element1" "element2")
```

## Initialization and Assignment

Values can be assigned using compound assignment, individual subscript assignment, or by appending.

```bash theme={"dark"}

# Compound assignment (indices 0, 1, 2)
arr=("alpha" "beta" "gamma")


# Specific index assignment (creates a sparse array)
arr[10]="omega"


# Appending to an existing array
arr+=("delta" "epsilon")
```

## Element Access via Parameter Expansion

Accessing array elements requires curly braces `{}` to prevent Bash from interpreting the subscript brackets as literal characters.

```bash theme={"dark"}

# Access a specific element
echo "${arr[0]}"


# Access all elements as separate words (preserves whitespace within elements)
echo "${arr[@]}"


# Access all elements as a single string (separated by the first character of IFS)
echo "${arr[*]}"
```

## Array Metadata

Bash provides specific parameter expansion syntax to retrieve the size of the array, the length of individual elements, and the populated indices.

```bash theme={"dark"}

# Get the total number of elements in the array
echo "${#arr[@]}"


# Get the string length of a specific element
echo "${#arr[0]}"


# Get the list of allocated indices (useful for sparse arrays)
echo "${!arr[@]}"
```

## Slicing

You can extract a subset of an array using the offset and length syntax. The offset represents the number of populated elements to skip, which does not necessarily correspond to the literal index due to the sparse nature of Bash arrays.

```bash theme={"dark"}

# Syntax: ${array_name[@]:offset:length}

# Extracts 2 elements starting from offset 1 (skips the first populated element)
slice=("${arr[@]:1:2}")
```

## Deletion

The `unset` built-in is used to remove individual elements or destroy the entire array. Removing an element does not shift the indices of subsequent elements; it merely deallocates that specific subscript. Array indices passed to `unset` must always be quoted to prevent unintended pathname expansion (globbing).

```bash theme={"dark"}

# Delete a specific element (creates a gap in the indices)

# Quotes prevent globbing hazards (e.g., matching a file named 'arr1' in the directory)
unset 'arr[1]'


# Delete the entire array
unset arr
```

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