Bash Script to Merge Two Arrays Easily
In Bash, you can merge two arrays by combining them like this:
merged_array=("${array1[@]}" "${array2[@]}") which creates a new array containing all elements from both arrays.Examples
Inputarray1=(1 2 3), array2=(4 5 6)
Outputmerged_array=(1 2 3 4 5 6)
Inputarray1=(apple banana), array2=(cherry date)
Outputmerged_array=(apple banana cherry date)
Inputarray1=(), array2=(x y z)
Outputmerged_array=(x y z)
How to Think About It
To merge two arrays in Bash, think of creating a new array that first takes all elements from the first array, then adds all elements from the second array. This is done by expanding both arrays with
"${array[@]}" syntax and putting them together inside parentheses.Algorithm
1
Define the first array with its elements.2
Define the second array with its elements.3
Create a new array by combining all elements from the first and second arrays using array expansion.4
Print or use the merged array as needed.Code
bash
#!/bin/bash array1=(1 2 3) array2=(4 5 6) merged_array=("${array1[@]}" "${array2[@]}") echo "Merged array elements: ${merged_array[@]}"
Output
Merged array elements: 1 2 3 4 5 6
Dry Run
Let's trace merging array1=(1 2 3) and array2=(4 5 6) through the code
1
Define array1
array1 contains: 1 2 3
2
Define array2
array2 contains: 4 5 6
3
Merge arrays
merged_array becomes: 1 2 3 4 5 6
| Step | merged_array contents |
|---|---|
| After step 3 | 1 2 3 4 5 6 |
Why This Works
Step 1: Array Expansion
Using "${array[@]}" expands all elements of an array preserving each element as a separate word.
Step 2: Combining Arrays
Putting both expanded arrays inside parentheses () creates a new array with all elements combined.
Step 3: Resulting Array
The new array contains all elements from the first array followed by all elements from the second array in order.
Alternative Approaches
Appending elements in a loop
bash
#!/bin/bash array1=(a b c) array2=(d e f) merged_array=() for element in "${array1[@]}" "${array2[@]}"; do merged_array+=("$element") done echo "${merged_array[@]}"
This method uses a loop to append elements one by one, which is more flexible but longer and slower for large arrays.
Using declare -a and indirect expansion
bash
#!/bin/bash declare -a array1=(1 2) declare -a array2=(3 4) merged_array=() merged_array=("${array1[@]}" "${array2[@]}") echo "${merged_array[@]}"
This is similar to the main method but explicitly declares arrays, useful for clarity and complex scripts.
Complexity: O(n) time, O(n) space
Time Complexity
Merging arrays requires visiting each element once, so time grows linearly with total elements.
Space Complexity
A new array is created to hold all elements, so space also grows linearly with input size.
Which Approach is Fastest?
Direct array expansion is fastest and simplest; looping is slower but more flexible for processing elements.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct array expansion | O(n) | O(n) | Simple merging with no processing |
| Appending in loop | O(n) | O(n) | Merging with element processing |
| Explicit declare -a | O(n) | O(n) | Clarity and complex scripts |
Always use double quotes around
${array[@]} to preserve elements with spaces when merging arrays.Forgetting quotes around
${array[@]} causes elements with spaces to split incorrectly during merging.