Can `+=` be used with arrays?

QuestionsQuestions8 SkillsProShell ArraysSep, 29 2025
096

Yes, the += operator can be used with arrays in Bash to append elements to an existing array.

Example of Appending to an Array:

# Declare an array
my_array=(1 2 3)

# Append an element
my_array+=(4)

# Append multiple elements
my_array+=(5 6)

# Print the array
echo "${my_array[@]}"  # Output: 1 2 3 4 5 6

In this example, the += operator is used to add elements to the my_array array.

0 Comments

no data
Be the first to share your comment!