Append Array to Another Array

To append an array to another array in Bash, use the following syntax.

</>
Copy
array1+=(${array2[@]})

All the elements of array2 shall be appended to array1.

Example

In the following script, we take two arrays: arr1 and arr2. We shall append the elements of array arr2 to array arr1.

Example.sh

</>
Copy
arr1=("apple" "banana" "cherry")
arr2=("mango" "grape")
arr1+=(${arr2[@]})
echo ${arr1[@]}

Output

apple banana cherry mango grape

Conclusion

In this Bash Tutorial, we have learnt how to append elements of an array to another array in Bash shell.