0
0
Bash Scriptingscripting~15 mins

Indexed array declaration in Bash Scripting - Deep Dive

Choose your learning style9 modes available
Overview - Indexed array declaration
What is it?
An indexed array in Bash is a list of values stored in a single variable, where each value has a number called an index. You can create an indexed array by assigning values to it using parentheses and spaces. Each value can be accessed by its index number, starting from zero. This helps organize multiple related pieces of data together.
Why it matters
Without indexed arrays, you would have to create many separate variables for related data, which is hard to manage and prone to errors. Indexed arrays let you group data logically, making scripts cleaner and easier to maintain. They also allow looping through data efficiently, which is essential for automation tasks.
Where it fits
Before learning indexed arrays, you should understand basic Bash variables and how to assign values. After mastering indexed arrays, you can learn associative arrays for key-value pairs and advanced array operations like slicing and looping.
Mental Model
Core Idea
An indexed array is like a numbered list where each item has a position number starting at zero, letting you store and access multiple values in one variable.
Think of it like...
Imagine a row of mailboxes numbered from 0 upwards, where each mailbox holds a letter. You can open any mailbox by its number to get the letter inside.
Indexed Array Structure:

  +---------+---------+---------+---------+
  | Index 0 | Index 1 | Index 2 | Index 3 |
  +---------+---------+---------+---------+
  | Value A | Value B | Value C | Value D |
  +---------+---------+---------+---------+

Access by index number inside square brackets, e.g., array[2] gives Value C.
Build-Up - 7 Steps
1
FoundationBasic array declaration syntax
🤔
Concept: How to declare an indexed array with multiple values in Bash.
You declare an indexed array by assigning values inside parentheses separated by spaces. For example: my_array=(apple banana cherry) This creates an array named my_array with three elements.
Result
The array my_array now holds three values: apple at index 0, banana at index 1, and cherry at index 2.
Understanding the simple syntax of parentheses and spaces is the first step to using arrays effectively in Bash.
2
FoundationAccessing array elements by index
🤔
Concept: How to get a specific value from an indexed array using its position number.
You access an element by writing the array name followed by the index in square brackets, like this: echo ${my_array[1]} This prints the second element because indexing starts at zero.
Result
banana
Knowing that array indexes start at zero helps avoid off-by-one errors when accessing elements.
3
IntermediateAdding elements to an array
🤔Before reading on: do you think you can add a new element by just assigning to a new index or do you need a special command? Commit to your answer.
Concept: How to add new elements to an existing indexed array by assigning to a new index.
You can add a new element by assigning a value to the next index: my_array[3]=date echo ${my_array[3]} This adds 'date' as the fourth element.
Result
date
Arrays in Bash are flexible; you can expand them by assigning to new indexes without re-declaring the whole array.
4
IntermediateGetting all array elements
🤔Before reading on: do you think using ${my_array} or ${my_array[@]} gives all elements? Commit to your answer.
Concept: How to retrieve all elements of an indexed array at once.
Use ${my_array[@]} to get all elements: echo ${my_array[@]} This prints all values separated by spaces.
Result
apple banana cherry date
Using ${array[@]} is the standard way to work with all array elements, especially in loops.
5
IntermediateDeclaring arrays with explicit indices
🤔
Concept: How to declare an array by assigning values to specific indexes explicitly.
You can assign values to specific indexes one by one: my_array[0]=apple my_array[2]=cherry my_array[5]=fig Indexes 1, 3, and 4 remain empty.
Result
my_array has values at indexes 0, 2, and 5; other indexes are empty or unset.
Explicit index assignment allows sparse arrays but requires careful index management.
6
AdvancedArray length and index retrieval
🤔Before reading on: do you think ${#array[@]} gives the number of elements or the highest index? Commit to your answer.
Concept: How to find the number of elements and all used indexes in an array.
Use ${#my_array[@]} to get the count of elements: echo ${#my_array[@]} Use ${!my_array[@]} to get all indexes: echo ${!my_array[@]} This helps when arrays have gaps.
Result
Count: 4 Indexes: 0 1 2 3
Knowing how to get array size and indexes helps avoid errors when looping or modifying arrays.
7
ExpertSparse arrays and unset elements behavior
🤔Before reading on: do you think unset elements in arrays cause errors or are simply skipped? Commit to your answer.
Concept: Understanding how Bash handles arrays with missing indexes and the effect of unsetting elements.
If you unset an element: unset my_array[1] That index is removed but others remain. Accessing unset indexes returns empty strings without errors. Loops over ${my_array[@]} skip unset elements, but loops over indexes include only set indexes from ${!my_array[@]}.
Result
Unset elements do not cause errors but are ignored in value loops; indexes reflect only set elements.
Understanding sparse arrays prevents bugs when elements are removed or skipped during iteration.
Under the Hood
Bash stores indexed arrays as a mapping from integer keys to string values internally. When you assign values, Bash records the index and value pairs. Accessing an element retrieves the value by its index key. Unset elements remove the key-value pair but do not shift other elements. The shell expands array expressions like ${array[@]} by iterating over stored keys in ascending order.
Why designed this way?
Bash arrays were designed to be simple and flexible for shell scripting needs. Using integer keys allows easy numeric indexing like lists in other languages. Sparse arrays support scripts that need non-contiguous data. The design avoids complex data structures to keep the shell lightweight and fast.
Indexed Array Internal View:

  +-------------------+
  | Bash Array Object |
  +-------------------+
          |
  +-----------------------------+
  | Index (int) -> Value (str)  |
  +-----------------------------+
  | 0 -> "apple"               |
  | 1 -> "banana"              |
  | 2 -> "cherry"              |
  | 3 -> "date"                |
  +-----------------------------+

Access: array[index] retrieves value by key.
Unset: removes key-value pair.
Myth Busters - 4 Common Misconceptions
Quick: Does ${array} give all elements or just the first? Commit to your answer.
Common Belief:Using ${array} without [@] or [*] returns all elements of the array.
Tap to reveal reality
Reality:Using ${array} returns only the first element of the array, not all elements.
Why it matters:Assuming ${array} gives all elements leads to scripts processing only one value instead of the full list, causing bugs.
Quick: If you unset an element, does the array shrink and shift indexes? Commit to your answer.
Common Belief:Unsetting an element removes it and shifts all later elements down to fill the gap.
Tap to reveal reality
Reality:Unsetting an element removes only that index; other elements keep their original indexes, so the array can become sparse.
Why it matters:Expecting automatic shifting causes wrong element access and logic errors in loops.
Quick: Can you use negative numbers as array indexes in Bash? Commit to your answer.
Common Belief:You can use negative numbers as valid array indexes like in some other languages.
Tap to reveal reality
Reality:Bash does not support negative indexes; only non-negative integers are valid indexes.
Why it matters:Trying to use negative indexes results in unexpected behavior or errors.
Quick: Does assigning to an index beyond the current highest index fill the gap with empty elements? Commit to your answer.
Common Belief:Assigning to a high index automatically fills all missing indexes with empty strings.
Tap to reveal reality
Reality:Bash does not fill missing indexes; gaps remain unset and are skipped in value expansions.
Why it matters:Assuming gaps are filled can cause incorrect assumptions about array length and contents.
Expert Zone
1
Assigning to an array index that is not sequential creates sparse arrays, which can affect looping behavior and length calculations.
2
Using ${array[@]} vs ${array[*]} differs in how elements are expanded, especially when quoted, affecting word splitting and iteration.
3
Unsetting elements does not reindex the array, so scripts relying on continuous indexes must handle gaps explicitly.
When NOT to use
Indexed arrays are not suitable when you need key-value pairs with non-numeric keys; use associative arrays instead. For very large datasets or complex data structures, consider external tools or languages better suited for data handling.
Production Patterns
In production scripts, indexed arrays are often used to store lists of filenames, command arguments, or configuration options. They are combined with loops to process each item. Sparse arrays are used carefully to track optional or conditional data. Quoting array expansions properly avoids word splitting bugs.
Connections
Associative arrays
Builds-on indexed arrays by allowing string keys instead of numeric indexes.
Understanding indexed arrays is essential before using associative arrays, which generalize the concept to key-value pairs.
Linked lists (data structures)
Shares the idea of storing multiple elements in order, but linked lists use pointers instead of indexes.
Knowing indexed arrays helps appreciate the tradeoffs between direct index access and pointer-based navigation in data structures.
Library cataloging systems
Both organize items with unique identifiers to quickly find and manage data.
Seeing arrays as catalog systems helps understand why indexes must be unique and stable for reliable access.
Common Pitfalls
#1Trying to print all array elements using ${array} instead of ${array[@]}
Wrong approach:echo ${my_array}
Correct approach:echo ${my_array[@]}
Root cause:Misunderstanding that ${array} returns only the first element, not the whole array.
#2Assuming unsetting an element shifts the array indexes
Wrong approach:unset my_array[1] echo ${my_array[1]} # expecting 'cherry' if it was at index 2
Correct approach:unset my_array[1] echo ${my_array[2]} # access remains at original index
Root cause:Believing arrays automatically reindex after element removal.
#3Using negative indexes to access array elements
Wrong approach:echo ${my_array[-1]}
Correct approach:echo ${my_array[${#my_array[@]}-1]} # to get last element
Root cause:Assuming Bash supports negative indexing like some other languages.
Key Takeaways
Indexed arrays in Bash store multiple values accessible by numeric positions starting at zero.
Use parentheses and spaces to declare arrays, and square brackets to access or assign elements by index.
Always use ${array[@]} to get all elements, not just ${array}, which returns only the first element.
Unsetting elements removes them without shifting indexes, so arrays can have gaps that affect loops and length.
Understanding indexed arrays is foundational for managing lists and sequences in Bash scripting effectively.