0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Print Array Elements Easily

In Bash, you can print all elements of an array using echo "${array[@]}" or loop through each element with for element in "${array[@]}"; do echo "$element"; done.
📋

Examples

Inputarray=(apple banana cherry)
Outputapple banana cherry
Inputarray=(dog cat "ice cream" "red apple")
Outputdog cat ice cream red apple
Inputarray=()
Output
🧠

How to Think About It

To print array elements in Bash, first understand that arrays hold multiple values. You can print all elements at once by expanding the array with ${array[@]}. Alternatively, you can loop through each element to print them one by one, which helps if you want each element on a new line or need to process them individually.
📐

Algorithm

1
Define the array with elements.
2
Use array expansion <code>${array[@]}</code> to access all elements.
3
Print all elements at once or loop through each element.
4
Inside the loop, print each element separately.
💻

Code

bash
#!/bin/bash

array=(apple banana cherry)

# Print all elements in one line
echo "${array[@]}"

# Print each element on a new line
for element in "${array[@]}"; do
  echo "$element"
done
Output
apple banana cherry apple banana cherry
🔍

Dry Run

Let's trace printing the array (apple banana cherry) through the code

1

Define array

array=(apple banana cherry)

2

Print all elements at once

echo "${array[@]}" outputs: apple banana cherry

3

Loop through elements

for element in "${array[@]}"; do echo "$element"; done outputs: apple banana cherry

IterationelementOutput
1appleapple
2bananabanana
3cherrycherry
💡

Why This Works

Step 1: Array Expansion

Using ${array[@]} expands all elements of the array, allowing you to access them together.

Step 2: Printing All Elements

Printing "${array[@]}" with echo shows all elements separated by spaces on one line.

Step 3: Looping Through Elements

The for loop iterates over each element, letting you print or process them individually.

🔄

Alternative Approaches

Using indices to print elements
bash
#!/bin/bash
array=(apple banana cherry)
for ((i=0; i<${#array[@]}; i++)); do
  echo "${array[i]}"
done
This method uses numeric indices to access elements, useful if you need the index during processing but is more verbose.
Print elements separated by newlines using printf
bash
#!/bin/bash
array=(apple banana cherry)
printf "%s\n" "${array[@]}"
Using printf prints each element on a new line without a loop, which is concise and efficient.

Complexity: O(n) time, O(n) space

Time Complexity

Printing all elements requires visiting each element once, so time grows linearly with the number of elements.

Space Complexity

The array occupies space proportional to its size; printing does not require extra significant memory.

Which Approach is Fastest?

Using echo "${array[@]}" is fastest for printing all elements at once; looping is flexible but slightly slower.

ApproachTimeSpaceBest For
echo "${array[@]}"O(n)O(n)Quick print all elements in one line
for loop over "${array[@]}"O(n)O(n)Processing or printing elements individually
printf with "${array[@]}"O(n)O(n)Printing each element on a new line efficiently
💡
Use "${array[@]}" to safely handle elements with spaces when printing or looping.
⚠️
Forgetting quotes around ${array[@]} can cause elements with spaces to split incorrectly.