Skip to main content

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.

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.

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

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

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

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

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

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

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

# Extract 2 elements starting from index 1
slice=("${my_indexed_array[@]:1:2}")
Master Bash with Deep Grasping Methodology!Learn More