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

A Bash array is a one-dimensional data structure capable of storing multiple values under a single variable identifier. Bash supports two types of arrays: **indexed arrays**, which use zero-based integers as keys, and **associative arrays** (introduced in Bash 4.0), which use arbitrary strings as keys. Bash arrays are dynamically sized, mutable, and do not require contiguous memory allocation.

## Declaration and Initialization

**Indexed Arrays**
Indexed arrays can be declared explicitly using the `declare` built-in or implicitly through assignment.

```bash theme={"dark"}

# Explicit declaration
declare -a my_indexed_array


# Implicit declaration via compound assignment
my_indexed_array=(value0 value1 value2)


# Sparse assignment (indices do not need to be sequential)
my_indexed_array[0]="first"
my_indexed_array[5]="sixth"
```

**Associative Arrays**
Associative arrays must be explicitly declared using the `-A` flag before they can be initialized or used.

```bash theme={"dark"}

# Explicit declaration (mandatory)
declare -A my_assoc_array


# Compound assignment
my_assoc_array=([key1]="value1" [key2]="value2")


# Individual assignment
my_assoc_array[key3]="value3"
```

## Accessing Elements

Accessing array elements requires parameter expansion using curly braces `{}` to prevent the shell from interpreting the index brackets as literal characters.

```bash theme={"dark"}

# Access a specific element
echo "${my_indexed_array[0]}"
echo "${my_assoc_array[key1]}"


# Access all elements
echo "${my_indexed_array[@]}"
echo "${my_indexed_array[*]}"
```

*Note on Expansion:* When enclosed in double quotes, `"${array[@]}"` expands each element as a separate word, preserving whitespace within elements. `"${array[*]}"` expands all elements into a single word, separated by the first character of the `IFS` (Internal Field Separator) variable.

## Modifying Arrays

**Appending Elements**
The `+=` operator appends elements to an indexed array without requiring knowledge of the current highest index.

```bash theme={"dark"}

# Append multiple elements
my_indexed_array+=(value3 value4)


# Append to an associative array
my_assoc_array+=([key4]="value4")
```

**Deleting Elements**
The `unset` built-in removes specific elements or the entire array from memory.

```bash theme={"dark"}

# Delete a specific element (creates a sparse array)
unset 'my_indexed_array[1]'
unset 'my_assoc_array[key1]'


# Delete the entire array
unset my_indexed_array
```

## Array Metadata

Bash provides specific parameter expansion syntax to retrieve metadata about the array, such as its size or its keys.

```bash theme={"dark"}

# Get the total number of elements in the array
length="${#my_indexed_array[@]}"


# Get the string length of a specific element
elem_length="${#my_indexed_array[0]}"


# Get all keys/indices currently in use
indices=("${!my_indexed_array[@]}")
keys=("${!my_assoc_array[@]}")
```

## Array Slicing

Indexed arrays support slicing via parameter expansion, allowing extraction of a subset of elements based on an offset and length.

```bash theme={"dark"}

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

# Extract 2 elements starting from index 1
slice=("${my_indexed_array[@]:1:2}")
```

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