Table of Contents
Using Shorthand Operator
Use the Shorthand operator to append one element to an array in Bash.
|
1 2 3 4 5 6 7 8 9 10 |
#Declare an array my_array=("element1" "element2" "element3") #Append a new element my_array+=("new_element") #Print the array echo "${my_array[@]}" |
|
1 2 3 |
element1 element2 element3 new_element |
First, we declared an array (my_array) with three initial elements element1, element2, and element3. Then, we used the shorthand operator (+=) to append the new_element to the end of an array. Finally, we used the echo command to print all the elements of the array ${my_array[@]}, including the newly appended one.
Use the Shorthand operator to append multiple elements to an array in Bash.
|
1 2 3 4 5 6 7 8 9 10 |
#Declare an array my_array=("element1" "element2" "element3") #Append multiple elements my_array+=("new_element1" "new_element2" "new_element3") #Print the array echo "${my_array[@]}" |
|
1 2 3 |
element1 element2 element3 new_element1 new_element2 new_element3 |
Again, we first declared an array my_array with three elements. Then we used the shorthand operator, +=, to append multiple elements/items to the end of the array in a single line. Finally, we used the echo command to print all the array elements to the console, including the newly appended ones.
Use the Shorthand operator to append the elements of one array to the other array in Bash.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#Declare an array my_array=("element1" "element2" "element3") #Declare another array other_array=("element4" "element5") #Append elements from another array my_array+=("${other_array[@]}") #Print the array echo "${my_array[@]}" |
|
1 2 3 |
element1 element2 element3 element4 element5 |
Here, an array named my_array is declared with three elements: element1, element2, and element3. Another array called other_array is also declared with two elements: element4 and element5. To append the elements from other_array to my_array, the shorthand operator (+=) is used. This method appends the array at once. Finally, we used the echo command to print all the elements of my_array.
Further reading:
Using for Loop
Use the for loop to append multiple elements to an array in Bash.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#Declare an array my_array=("element1" "element2" "element3") #Declare new elements in a separate array new_elements=("new_element1" "new_element2" "new_element3") #Append new elements using a loop for element in "${new_elements[@]}"; do my_array+=("$element") done #Print the array echo "${my_array[@]}" |
|
1 2 3 |
element1 element2 element3 new_element1 new_element2 new_element3 |
In this method, first, we declared an array named my_array and initialized it with three elements. After that, we declared a separate array called new_elements initialized with three new elements. Next, we used a for loop to iterate over each element in the new_elementsarray.
The loop variable element takes the value of each element in new_elements in each iteration. Inside the loop, the += operator is used to append the current element to the my_array. In this way, each new element is added to the existing my_array array. Finally, we used the echo command to print the elements of the my_array.
Using array[index]=value Syntax
Use array[index]=value syntax to append an element to an array in Bash.
|
1 2 3 4 5 6 7 8 9 10 |
#Declare an array my_array=("element1" "element2") #Append a new element at index 3 my_array[2]="new_element" #Print the array echo "${my_array[@]}" |
|
1 2 3 |
element1 element2 new_element |
This is one of the simplest ways to add new elements to an array. In this method, we first declare an array with two elements; as an array’s index starts with zero, the new element can be added at the second index of the array.
The array[index]=value syntax is used to add a third element to the array. Next, the value new_element is assigned to the third index of the array, and finally, the echo command prints all the elements of the updated array to the console.
Using declare Command
Use the declare command with the -a option to append an element to the specified array in Bash.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#Declare an array my_array=("element1" "element2" "element3") #Declare a new element new_element="new_element" #Append the new element using declare declare -a my_array+=("$new_element") #Print the array echo "${my_array[@]}" |
|
1 2 3 |
element1 element2 element3 new_element |
First, an array named my_array is initialized with three string-type elements, while a variable new_element is set to the new element we wanted to append to the array. Then, to append the new element to the array, the declare command is used along with the shorthand operator. Finally, the updated array is displayed on the console.
Using mapfile Command
Use the mapfile command to append multiple elements to an array in Bash. To use this approach, you must have Bash version 4 or above.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#Declare an array my_array=("element1" "element2" "element3") #Declare new elements as separate lines in a string new_elements="new_element1 new_element2 new_element3" #Append new elements using mapfile mapfile -t -O "${#my_array[@]}" my_array <<< "$new_elements" #Print the array echo "${my_array[@]}" |
|
1 2 3 |
element1 element2 element3 new_element1 new_element2 new_element3 |
After creating an array (my_array) with three elements and a variable (new_elements) containing a multiline string, we used the mapfile command to read the string from new_elements and assign them to the my_array; here -t was used to remove trailing newline characters.
The -O option is used to specify the starting index for appending the new elements. In this case, we use ${#my_array[@]} to append the elements at the end of the array. Next, the <<< syntax is used to redirect the contents of the new_elements as input to mapfile. We finally echoed the updated array to the console.
Using custom function push() Function
Use the custom function push() function to append multiple elements to the array in Bash.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
push() { local -n array=$1 shift for element in "$@"; do array+=("$element") done } #Declare an array my_array=("element1" "element2" "element3") #Call the push() function to append elements push my_array "new_element1" "new_element2" #Print the array echo "${my_array[@]}" |
|
1 2 3 |
element1 element2 element3 new_element1 new_element2 |
First, a custom function push() is defined, which appends elements to an array.
This function takes the array name as the first argument ($1) and shifts it from the argument list. Then, we used a for loop to loop over the remaining arguments ($@) and append each element to the array using a shorthand operator.
Finally, in the main function, an array my_array is declared with three initial elements. Then, the push() function is called by passing the array name, my_array, as the first argument, followed by the elements new_element1 and new_element2 we want to append. Finally, we used echo to print all the elements of the updated array.
That’s all about Bash Append to Array.