The array is termed as the set of the same types of elements. But a Bash array is not a set of similar elements despite other scripting languages. We can use the different types of elements like strings and integers in the Bash array as the Bash does not distinguish between them. Note that the multidimensional arrays are not supported by Bash, inserting an array inside another array is not possible. We can only use a one-dimensional index array and associative array in Bash. We demonstrate the Bash array declaration, initialization, modification, iteration, and deletion with the following examples:
Example 1: Bash Index Array in Ubuntu 22.04
The index array is the one-dimensional array, and we can term the index array as a list. When the keys(index) of an array are in the ordered integers, they are referred to as index arrays.
We declare the array with the “declare” keyword with a “-a” flag that is used for the lowercase. We label the array name as “myArr” and then insert some strings. The echo command is used to display the elements of the array. All the elements are printed as we utilize the “@” option. Then, we also display the specific index element by specifying the index value to the echo command.
The following shows each element of the Bash array. The elements from the specific indices of an array are also displayed.
Example 2: Bash Associative Array in Ubuntu 22.04
The associative Bash array is called the Hash table or dictionaries. The associative array is an ordered list of items whose keys are represented as strings. Note that we can only declare the associative array with the built-in “declare” term in Bash while this is not compulsory for the index arrays.
We use the “declare” modifier with a “-A” flag which is used for the uppercase in Bash. Then, we set the attribute name “COUNTRIES_CITIES”. The attribute represents the associative array. We create an associative array by assigning the value against each string of keys. Each string key of the array holds the string value that we display with the echo statement. The option “@” used instead of the index value prints all the lists of the array.
The Bash associative array declaration returns the following results:
Example 3: Bash Iterating Array Over Loops in Ubuntu 22.04
Iterating arrays are the very common methods that the Bash programs also support. While iterating, we simply loop over a sequence of certain events until the predetermined condition is met. First, we see the iteration through the “for in” loop. The Bash array’s object value is iterated over by the “for in” loop.
We define the Bash array attribute as “names”. The array “names” is assigned with the random names of people. After that, we have the “for…in” loop for the iteration of the Bash array. The object “n” is declared which is the key for the returned value. The loop repeats until all the elements of the object “n” are used. Upon each iteration, the echo command displays the names from the Bash array.
The “for…in” loop prints all the names from the Bash array individually as seen inside the terminal.
Now, we use the “for…loop” that takes the index and repeats it till the length of the Bash array. Throughout each iteration of the loop, the index is utilized to retrieve the array’s elements. We create the Bash array as “ArrElem” which is initialized with three string elements. Then, we use the “for….loop” which iterates over the end of the array element until the condition is satisfied. The echo command calls the Bash array which holds the object “n” which is declared inside the “for…loop”. The object “n” has all the iterated elements of the Bash array.
The Bash array iterates the elements for the “for…loop” and is returned in the output Bash screen.
We have seen the functionality of the “for in” loop and the “for…loop” for iterating the Bash array. Now, we use the while loop that cycles over the starting index and stops at the index which is less than the array length. Each iteration of the while loop allows us to access the array item at the given index.
The “num” is declared as the Bash array which has integers. The object “x” is defined as the array index position. The while loop calls this object inside it for the iteration that starts from the index=0 and executes until the condition is met. Each time the condition is true, the echo statement between the do and done will execute and the object “x” also increments at the same time.
The following elements are returned from the while loop over the Bash array:
Example 4: Bash Appends an Array in Ubuntu 22.04
We can insert the element in the Bash array by using the “+=” operator as Bash provides no built-in function. By using this operator, we can add the new element at the end of the Bash array.
Here, we construct the array of strings which is labeled as “Array”. The initial array has only three elements which we print with the echo command. Then, we use the “+=” operator with the attribute “Array” to which the array is assigned. We add the two string elements in that array and display the new appended array with the echo function.
We can see in the following output that the previous Bash array and the new Bash array elements are appended at the end.
Example 5: Bash Array Length in Ubuntu 22.04
The “#BashArray[@]” expression can be used to retrieve the Bash array’s length. The hashtag “#” symbol calculates the array length. The array length means all the elements in the Bash array.
First, we create the empty Bash array inside the “BashArray” variable. Then, we use the “+” operator to add the string element inside the Bash array. We add two string elements in the empty Bash array, respectively. Then, we use the hashtag “#” symbol with the “BashArray” variable for the length of the Bash array.
Since only two elements exist in the Bash array, the length of the returned array is “2” in the following compilation:
Example 6: Bash Unset Array in Ubuntu 22.04
To delete the Bash array or specific element from the Bash array index, we use the built-in function “unset” of the Bash. The unset function assigns the null value to the element of the Bash array.
We declare the “elements” array by using the “-a” option and the string elements are added to that Bash array. Then, we employ the unset command to delete the Bash array “elements” index value “3”.
The index value “3” from the Bash array is deleted and the remaining Bash array elements are displayed.
Next, we delete the entire Bash array using the Bash unset command.
This time, we have just given a Bash array name “elements” to the unset command instead of specifying the index values. The entire Bash array is removed as the null value are assigned to them.
The Bash array elements are now replaced with the null value as shown in the snap of the Bash program compilation.
Bash arrays are strong data structures that can be quite helpful. We can systematize the various operations with Bash language and arrays, which can save hundreds of hours of manual tasks. Here, we covered the various Bash array operations using the Linux server Ubuntu 22.04. We declared and initialized the index and associative Bash array. Then, we used the looping methods over the Bash arrays. We used the Bash array to append the element and unset the Bash array with the unset command.