{"id":18458,"date":"2022-09-17T05:18:17","date_gmt":"2022-09-17T05:18:17","guid":{"rendered":"https:\/\/linuxways.net\/?p=18458"},"modified":"2022-09-20T14:23:28","modified_gmt":"2022-09-20T14:23:28","slug":"bash-array-tutorial","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/bash-programming\/bash-array-tutorial\/","title":{"rendered":"Bash Array Tutorial"},"content":{"rendered":"<p>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:<\/p>\n<h2><strong>Example 1: Bash Index Array in Ubuntu 22.04<\/strong><\/h2>\n<p>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.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"173\" class=\"wp-image-18460\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-1.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-1.jpeg 682w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-1-300x76.jpeg 300w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/p>\n<p>We declare the array with the \u201cdeclare\u201d keyword with a \u201c-a\u201d flag that is used for the lowercase. We label the array name as \u201cmyArr\u201d 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 \u201c@\u201d option. Then, we also display the specific index element by specifying the index value to the echo command.<\/p>\n<p>The following shows each element of the Bash array. The elements from the specific indices of an array are also displayed.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"632\" height=\"109\" class=\"wp-image-18462\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-2.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-2.jpeg 632w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-2-300x52.jpeg 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/strong><\/p>\n<h2><strong>Example 2: Bash Associative Array in Ubuntu 22.04<\/strong><\/h2>\n<p>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 \u201cdeclare\u201d term in Bash while this is not compulsory for the index arrays.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"179\" class=\"wp-image-18463\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-3.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-3.jpeg 705w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-3-300x76.jpeg 300w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/strong><\/p>\n<p>We use the \u201cdeclare\u201d modifier with a \u201c-A\u201d flag which is used for the uppercase in Bash. Then, we set the attribute name \u201cCOUNTRIES_CITIES\u201d. 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 \u201c@\u201d used instead of the index value prints all the lists of the array.<\/p>\n<p>The Bash associative array declaration returns the following results:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"39\" class=\"wp-image-18465\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-4.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-4.jpeg 639w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-4-300x18.jpeg 300w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/strong><\/p>\n<h2><strong>Example 3: Bash Iterating Array Over Loops in Ubuntu 22.04<\/strong><\/h2>\n<p>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 \u201cfor in\u201d loop. The Bash array&#8217;s object value is iterated over by the &#8220;for in&#8221; loop.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"567\" height=\"166\" class=\"wp-image-18468\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-5.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-5.jpeg 567w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-5-300x88.jpeg 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/strong><\/p>\n<p>We define the Bash array attribute as \u201cnames\u201d. The array \u201cnames\u201d is assigned with the random names of people. After that, we have the \u201cfor\u2026in\u201d loop for the iteration of the Bash array. The object \u201cn\u201d is declared which is the key for the returned value. The loop repeats until all the elements of the object \u201cn\u201d are used. Upon each iteration, the echo command displays the names from the Bash array.<\/p>\n<p>The \u201cfor\u2026in\u201d loop prints all the names from the Bash array individually as seen inside the terminal.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"128\" class=\"wp-image-18470\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-6.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-6.jpeg 652w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-6-300x59.jpeg 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/strong><\/p>\n<p>Now, we use the \u201cfor\u2026loop\u201d 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&#8217;s elements. We create the Bash array as \u201cArrElem\u201d which is initialized with three string elements. Then, we use the \u201cfor\u2026.loop\u201d 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 \u201cn\u201d which is declared inside the \u201cfor\u2026loop\u201d. The object \u201cn\u201d has all the iterated elements of the Bash array.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"174\" class=\"wp-image-18475\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-7.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-7.jpeg 680w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-7-300x77.jpeg 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/strong><\/p>\n<p>The Bash array iterates the elements for the \u201cfor\u2026loop\u201d and is returned in the output Bash screen.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"656\" height=\"92\" class=\"wp-image-18478\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-8.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-8.jpeg 656w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-8-300x42.jpeg 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/strong><\/p>\n<p>We have seen the functionality of the \u201cfor in\u201d loop and the \u201cfor\u2026loop\u201d 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.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"235\" class=\"wp-image-18481\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-9.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-9.jpeg 670w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-9-300x105.jpeg 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/strong><\/p>\n<p>The \u201cnum\u201d is declared as the Bash array which has integers. The object \u201cx\u201d 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 \u201cx\u201d also increments at the same time.<\/p>\n<p>The following elements are returned from the while loop over the Bash array:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"132\" class=\"wp-image-18484\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-10.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-10.jpeg 639w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-10-300x62.jpeg 300w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/strong><\/p>\n<h2><strong>Example 4: Bash Appends an Array in Ubuntu 22.04<\/strong><\/h2>\n<p>We can insert the element in the Bash array by using the \u201c+=\u201d 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.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"655\" height=\"175\" class=\"wp-image-18489\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-11.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-11.jpeg 655w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-11-300x80.jpeg 300w\" sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><\/strong><\/p>\n<p>Here, we construct the array of strings which is labeled as \u201cArray\u201d. The initial array has only three elements which we print with the echo command. Then, we use the \u201c+=\u201d operator with the attribute \u201cArray\u201d 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.<\/p>\n<p>We can see in the following output that the previous Bash array and the new Bash array elements are appended at the end.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"90\" class=\"wp-image-18490\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-12.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-12.jpeg 680w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-12-300x40.jpeg 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/strong><\/p>\n<h2><strong>Example 5: Bash Array Length in Ubuntu 22.04<\/strong><\/h2>\n<p>The &#8220;#BashArray[@]&#8221; expression can be used to retrieve the Bash array&#8217;s length. The hashtag \u201c#\u201d symbol calculates the array length. The array length means all the elements in the Bash array.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"141\" class=\"wp-image-18496\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-13.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-13.jpeg 710w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-13-300x60.jpeg 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/strong><\/p>\n<p>First, we create the empty Bash array inside the \u201cBashArray\u201d variable. Then, we use the \u201c+\u201d 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 \u201c#\u201d symbol with the \u201cBashArray\u201d variable for the length of the Bash array.<\/p>\n<p>Since only two elements exist in the Bash array, the length of the returned array is \u201c2\u201d in the following compilation:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"701\" height=\"57\" class=\"wp-image-18497\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-14.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-14.jpeg 701w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-14-300x24.jpeg 300w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><\/strong><\/p>\n<h2><strong>Example 6: Bash Unset Array in Ubuntu 22.04<\/strong><\/h2>\n<p>To delete the Bash array or specific element from the Bash array index, we use the built-in function \u201cunset\u201d of the Bash. The unset function assigns the null value to the element of the Bash array.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"814\" height=\"124\" class=\"wp-image-18499\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-15.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-15.jpeg 814w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-15-300x46.jpeg 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-15-768x117.jpeg 768w\" sizes=\"auto, (max-width: 814px) 100vw, 814px\" \/><\/strong><\/p>\n<p>We declare the \u201celements\u201d array by using the \u201c-a\u201d option and the string elements are added to that Bash array. Then, we employ the unset command to delete the Bash array \u201celements\u201d index value \u201c3\u201d.<\/p>\n<p>The index value \u201c3\u201d from the Bash array is deleted and the remaining Bash array elements are displayed.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"689\" height=\"56\" class=\"wp-image-18501\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-16.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-16.jpeg 689w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-16-300x24.jpeg 300w\" sizes=\"auto, (max-width: 689px) 100vw, 689px\" \/><\/strong><\/p>\n<p>Next, we delete the entire Bash array using the Bash unset command.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"832\" height=\"107\" class=\"wp-image-18502\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-17.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-17.jpeg 832w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-17-300x39.jpeg 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-17-768x99.jpeg 768w\" sizes=\"auto, (max-width: 832px) 100vw, 832px\" \/><\/strong><\/p>\n<p>This time, we have just given a Bash array name \u201celements\u201d to the unset command instead of specifying the index values. The entire Bash array is removed as the null value are assigned to them.<\/p>\n<p>The Bash array elements are now replaced with the null value as shown in the snap of the Bash program compilation.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"673\" height=\"56\" class=\"wp-image-18503\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-18.jpeg\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-18.jpeg 673w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/09\/word-image-18458-18-300x25.jpeg 300w\" sizes=\"auto, (max-width: 673px) 100vw, 673px\" \/><\/strong><\/p>\n<h2><strong>Conclusion <\/strong><\/h2>\n<p>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.<\/p>","protected":false},"excerpt":{"rendered":"<p>Tutorial on the various Bash array operations using the Linux server Ubuntu 22.04 by declaring and initializing the index and associative Bash array.<\/p>","protected":false},"author":105,"featured_media":18676,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1050],"tags":[],"class_list":["post-18458","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-programming"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/105"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=18458"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18458\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18676"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=18458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=18458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=18458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}