Bash Append to Array

Bash Append to Array

Using Shorthand Operator

Use the Shorthand operator to append one element to an array in Bash.

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.

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.

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.

Using for Loop

Use the for loop to append multiple elements to an array in Bash.

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.

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.

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.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *