Skip to main content
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.

Initialization and Assignment

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

Element Access via Parameter Expansion

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

Array Metadata

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

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.

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).
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More