0
0
Bash Scriptingscripting~15 mins

Iterating over arrays in Bash Scripting - Deep Dive

Choose your learning style9 modes available
Overview - Iterating over arrays
What is it?
Iterating over arrays means going through each item in a list one by one. In bash scripting, arrays hold multiple values, and iteration lets you access or use each value in order. This helps automate tasks that need to handle many pieces of data. Without iteration, you would have to write repetitive code for each item.
Why it matters
Without the ability to loop over arrays, scripts would be long, repetitive, and hard to maintain. Iteration lets you write simple, reusable code that can handle any number of items. This saves time and reduces errors when automating tasks like processing files, managing lists, or running commands on multiple inputs.
Where it fits
Before learning array iteration, you should understand basic bash commands, variables, and how to create arrays. After mastering iteration, you can learn advanced looping techniques, conditional logic inside loops, and how to manipulate array elements dynamically.
Mental Model
Core Idea
Iterating over arrays is like checking each item in a shopping list one by one to decide what to do with it.
Think of it like...
Imagine you have a basket of fruits. To eat or sort them, you pick each fruit one at a time and decide what to do. Iteration in arrays is the same: you pick each element in order and process it.
Array: [item1, item2, item3, ..., itemN]
Iteration flow:
┌─────────┐
│ Start   │
└────┬────┘
     │
     ▼
┌─────────┐    ┌─────────┐    ┌─────────┐    ┌─────────┐
│ item1   │ -> │ item2   │ -> │ item3   │ -> │ ...     │
└─────────┘    └─────────┘    └─────────┘    └─────────┘
     │
     ▼
  Process each item
     │
     ▼
┌─────────┐
│ End     │
└─────────┘
Build-Up - 7 Steps
1
FoundationCreating and accessing bash arrays
🤔
Concept: Learn how to define an array and access its elements by index.
In bash, you create an array by listing values inside parentheses: fruits=(apple banana cherry) Access elements by their position (starting at 0): echo ${fruits[0]} # prints apple echo ${fruits[2]} # prints cherry
Result
apple cherry
Knowing how to create and access arrays is the base for iterating over them. Without this, you can't work with multiple values efficiently.
2
FoundationBasic for loop syntax in bash
🤔
Concept: Understand how to write a simple for loop to repeat commands.
A for loop runs commands multiple times. Example: for i in 1 2 3; do echo "Number $i" done This prints each number on its own line.
Result
Number 1 Number 2 Number 3
Mastering the for loop structure is essential because iteration over arrays uses this pattern to process each element.
3
IntermediateIterating over array elements directly
🤔Before reading on: do you think you can loop over array elements by name or only by index? Commit to your answer.
Concept: Learn to loop over all elements in an array without manually using indexes.
Use this syntax to loop over all elements: for fruit in "${fruits[@]}"; do echo "I like $fruit" done This prints each fruit in the array.
Result
I like apple I like banana I like cherry
Understanding that "${array[@]}" expands to all elements lets you write clean loops without worrying about indexes.
4
IntermediateIterating using array indexes
🤔Before reading on: is it better to loop over indexes or elements directly? Commit to your answer.
Concept: Learn to loop over array indexes to access elements and their positions.
You can loop over indexes like this: for i in "${!fruits[@]}"; do echo "Fruit $i is ${fruits[$i]}" done "${!fruits[@]}" gives all valid indexes.
Result
Fruit 0 is apple Fruit 1 is banana Fruit 2 is cherry
Knowing how to loop over indexes is useful when you need the position or want to modify elements.
5
IntermediateHandling arrays with spaces in elements
🤔Before reading on: do you think array elements with spaces need special handling in loops? Commit to your answer.
Concept: Learn how to safely iterate arrays when elements contain spaces.
Define array with quotes: files=("file one.txt" "file two.txt" "file three.txt") Loop with quotes to preserve spaces: for file in "${files[@]}"; do echo "Processing $file" done
Result
Processing file one.txt Processing file two.txt Processing file three.txt
Quoting "${array[@]}" preserves each element as a whole, preventing bugs with spaces.
6
AdvancedModifying array elements during iteration
🤔Before reading on: can you change array elements inside a for loop directly? Commit to your answer.
Concept: Learn how to update array elements while looping using indexes.
You cannot modify elements when looping over values directly. Use indexes: for i in "${!fruits[@]}"; do fruits[$i]="${fruits[$i]}_fresh" done echo "${fruits[@]}"
Result
apple_fresh banana_fresh cherry_fresh
Knowing the difference between looping over values and indexes is key to safely modifying arrays.
7
ExpertIterating arrays with nested loops and complex data
🤔Before reading on: do you think bash arrays can hold complex data like other arrays? Commit to your answer.
Concept: Explore nested loops and handling arrays of arrays using bash associative arrays or string tricks.
Bash does not support true multi-dimensional arrays. You can simulate with naming: matrix_row0=(1 2 3) matrix_row1=(4 5 6) for row in 0 1; do for col in "${!matrix_row${row}[@]}"; do eval val="\${matrix_row${row}[$col]}" echo "Row $row Col $col = $val" done done
Result
Row 0 Col 0 = 1 Row 0 Col 1 = 2 Row 0 Col 2 = 3 Row 1 Col 0 = 4 Row 1 Col 1 = 5 Row 1 Col 2 = 6
Understanding bash's limitations and workarounds for complex data structures helps write powerful scripts despite language constraints.
Under the Hood
Bash arrays are stored as indexed lists of strings internally. When you use "${array[@]}", bash expands this to all elements separated by spaces, preserving element boundaries if quoted. The for loop then iterates over these expanded words one by one. Index expansion "${!array[@]}" returns all valid indexes as strings. Modifying elements requires referencing the array by index because looping over values gives copies, not references.
Why designed this way?
Bash was designed as a simple shell language for command execution, not complex data handling. Arrays were added later with minimal overhead, focusing on string lists. This design favors simplicity and speed over complex data structures. Alternatives like associative arrays were added later to extend functionality without breaking backward compatibility.
┌─────────────┐
│ Bash Array  │
│ [0] "apple" │
│ [1] "banana"│
│ [2] "cherry"│
└─────┬───────┘
      │
      ▼
┌───────────────────────────────┐
│ "${array[@]}" expands to:     │
│ "apple" "banana" "cherry" │
└─────────────┬─────────────────┘
              │
              ▼
┌───────────────────────────────┐
│ For loop iterates over words   │
│ one by one, assigning to var   │
└───────────────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does "${array[@]}" without quotes preserve elements with spaces? Commit yes or no.
Common Belief:People often believe that "${array[@]}" without quotes keeps elements with spaces intact.
Tap to reveal reality
Reality:Without quotes, "${array[@]}" splits elements on spaces, breaking elements that contain spaces into multiple words.
Why it matters:This causes bugs where file names or strings with spaces are split incorrectly, leading to errors or data loss.
Quick: Can you modify array elements by looping over values directly? Commit yes or no.
Common Belief:Some think looping over array values lets you change the original array elements directly.
Tap to reveal reality
Reality:Looping over values gives copies, so modifying the loop variable does not change the array elements.
Why it matters:This misunderstanding leads to scripts that appear to modify arrays but silently fail, causing incorrect results.
Quick: Does bash support true multi-dimensional arrays natively? Commit yes or no.
Common Belief:Many believe bash arrays can hold other arrays as elements, like multi-dimensional arrays.
Tap to reveal reality
Reality:Bash arrays hold only strings; multi-dimensional arrays must be simulated with naming conventions or associative arrays.
Why it matters:Assuming native multi-dimensional arrays leads to confusing code and bugs when trying to nest arrays.
Quick: Does "${!array[@]}" give array values or indexes? Commit your answer.
Common Belief:Some think "${!array[@]}" returns the array values.
Tap to reveal reality
Reality:"${!array[@]}" returns the list of valid indexes (keys) of the array.
Why it matters:Confusing indexes with values causes loops to behave unexpectedly, often printing indexes instead of elements.
Expert Zone
1
When iterating, quoting "${array[@]}" preserves element boundaries, but quoting "${array[*]}" treats the entire array as one string, which changes behavior.
2
Using "${!array[@]}" to get indexes is essential for modifying elements during iteration, but it can be tricky with sparse arrays where indexes are not continuous.
3
Bash arrays are zero-based but can have non-numeric or sparse indexes in associative arrays, which changes how iteration works.
When NOT to use
For very large datasets or complex data structures, bash arrays and loops become inefficient and hard to maintain. In such cases, use higher-level languages like Python or Perl that support advanced data types and faster iteration.
Production Patterns
In production scripts, iterating over arrays is often combined with conditional checks, string manipulation, and external command calls. Experts use indexed loops to update arrays, handle errors gracefully, and optimize loops by minimizing subshells and external calls.
Connections
Linked Lists (Data Structures)
Both involve sequential access to elements one by one.
Understanding iteration in arrays helps grasp how linked lists traverse nodes sequentially, reinforcing the concept of processing items in order.
Batch Processing in Operating Systems
Iterating over arrays is like batch processing multiple jobs or tasks sequentially.
Knowing how iteration works in scripting clarifies how operating systems schedule and execute batches of tasks one after another.
Assembly Language Loops
Low-level loops in assembly iterate over memory addresses similar to array iteration in bash.
Recognizing this connection deepens understanding of how high-level loops map to machine instructions accessing sequential memory.
Common Pitfalls
#1Breaking array elements with spaces by not quoting expansions.
Wrong approach:for file in ${files[@]}; do echo "$file" done
Correct approach:for file in "${files[@]}"; do echo "$file" done
Root cause:Not quoting "${array[@]}" causes bash to split elements on spaces, breaking multi-word elements.
#2Trying to modify array elements by looping over values instead of indexes.
Wrong approach:for fruit in "${fruits[@]}"; do fruit="fresh_$fruit" done echo "${fruits[@]}"
Correct approach:for i in "${!fruits[@]}"; do fruits[$i]="fresh_${fruits[$i]}" done echo "${fruits[@]}"
Root cause:Loop variable is a copy of the element, so changing it does not affect the original array.
#3Assuming bash supports multi-dimensional arrays natively.
Wrong approach:matrix=((1 2) (3 4)) for row in "${matrix[@]}"; do echo "$row" done
Correct approach:matrix_row0=(1 2) matrix_row1=(3 4) for row in 0 1; do for col in "${!matrix_row${row}[@]}"; do eval val="\${matrix_row${row}[$col]}" echo "$val" done done
Root cause:Bash arrays hold strings only; nested arrays must be simulated with separate arrays and naming.
Key Takeaways
Iterating over arrays in bash lets you process multiple items efficiently with simple loops.
Always quote "${array[@]}" to preserve elements with spaces during iteration.
Use "${!array[@]}" to loop over indexes when you need to modify array elements.
Bash arrays are simple lists of strings; complex data structures require workarounds.
Understanding these basics prevents common bugs and unlocks powerful automation scripts.