Concept

Arrays

Definition

An array is an ordered collection of values stored under a single variable name, where each element is reached by an integer index. The position is the address. The first element is conventionally at index zero in most modern languages, though some older languages use one-based indexing. Arrays are the simplest non-trivial data structure and the building block on which almost every other collection — lists, queues, stacks, ring buffers, matrices — is implemented.

In bash, arrays appear with a parenthesised assignment such as names=(alice bob carol) and are read element-by-element with ${names[0]} or expanded in their entirety with ${names[@]}. The structure is identical in spirit across languages, even when the surface syntax changes dramatically.

Why it matters

How it works

Internally, a classical array is a contiguous block of memory: the address of element N is the base address plus N times the element size. That arithmetic gives constant-time random access, the property that makes arrays so foundational. Insertion at the front or middle, however, requires shifting every following element, which is why operations that mutate the structure favour different shapes — linked lists for cheap mid-stream insertion, ring buffers for cheap rotation, hash tables for cheap lookup by name rather than position.

The limits matter. A fixed-size array commits to a length at allocation time; a dynamic array (the kind most scripting languages expose by default) hides a resize-and-copy step behind the scenes when capacity is exceeded. Some bash idioms also treat strings as character arrays via substring expansion, but bash arrays themselves are properly typed and recommended for any script that handles a list of items — never split a delimited string with word-splitting tricks when an array would do.

Where it goes next

Continue exploring

Tags