{"id":54623,"date":"2022-05-13T15:09:26","date_gmt":"2022-05-13T15:09:26","guid":{"rendered":"\/tutorials\/?p=54623"},"modified":"2026-03-10T09:29:01","modified_gmt":"2026-03-10T09:29:01","slug":"how-to-use-bash-array","status":"publish","type":"post","link":"\/ca\/tutorials\/how-to-use-bash-array","title":{"rendered":"How to use Bash array in scripting: A complete guide"},"content":{"rendered":"<p>A Bash array is a structure in the Bash programming language that lets you group and index items, which can be strings or numbers. It helps you work with a lot of data more efficiently, streamlining various UNIX system administration tasks like storing command outputs. <\/p><p>You can write Bash arrays in two ways: indexed and associative. While indexed arrays assign a number to an element, the associative counterpart uses strings and requires the explicit declare shell builtin. Apart from that, you can write them using the same syntax.&nbsp;<\/p><p>When using arrays in Bash scripts, you will often modify or channel them through different operations, such as:<\/p><ul class=\"wp-block-list\">\n<li>Adding a new variable to an existing Bash array<\/li>\n\n\n\n<li>Referencing and printing a specific array element<\/li>\n\n\n\n<li>Removing an element from a Bash array<\/li>\n\n\n\n<li>Looping through items in a Bash array<\/li>\n\n\n\n<li>Passing a Bash array into a function.<\/li>\n<\/ul><p>Let&rsquo;s dive deeper into using Bash arrays in scripting, their operations, and practical use case examples.<\/p><p>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-what-is-a-bash-array\">What is a Bash array?<\/h2><p>A Bash array is a data structure designed to store information in an indexed way. In other words, a Bash array is a large group of variables. Unlike typical arrays used in other programming languages, arrays can store different types of elements. For example, you can use a bash array to store both strings and numbers.<\/p><p>Note that bash does not support multidimensional arrays, so it&rsquo;s not possible to add an array within an array.<\/p><p>There are two types of bash arrays: <strong>indexed <\/strong>and <strong>associative<\/strong>. An<strong> indexed<\/strong> array is referred to by integers or numbers, which helps organize basic data. Meanwhile, an<strong> associative<\/strong> array is referred to via strings or a set of characters and words, suitable for storing key-value pairs.<\/p><p>Apart from the reference, the method of writing indexed and associative arrays also slightly differs. <\/p><h2 class=\"wp-block-heading\" id=\"h-how-do-you-declare-a-bash-array\">How do you declare a Bash array?<\/h2><p>Declaring a Bash array essentially means creating the index and assigning elements to it. You can also explicitly declare an array by adding the shell builtin <strong>declare<\/strong> with the <strong>-a<\/strong> flag at the beginning of your Bash script, though it&rsquo;s optional in some scenarios.<\/p><p>For example, declaring an indexed array doesn&rsquo;t need the shell builtin, but it&rsquo;s mandatory if you want to declare an associative array. Apart from this rule, you can declare either arrays using the same syntax, which we&rsquo;ll explain next. <\/p><h3 class=\"wp-block-heading\" id=\"h-syntax-overview\">Syntax overview<\/h3><p>The basic syntax of a Bash array looks like the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#optional for indexed\ndeclare -a IndexedArray\nArray[subscript1]=element1\nArray[subscript2]=element2\nArray[subscript2]=element3<\/pre><p><strong>Subscript<\/strong> is the reference to your array elements, which are <strong>numbers<\/strong> for indexed arrays and <strong>strings<\/strong> for the associative ones. Here&rsquo;s an example of an actual indexed Bash array:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#optional for indexed\ndeclare -a IndexedArray\nIndexedArray[0]=car\nIndexedArray[1]=plane\nIndexedArray[2]=bike<\/pre><p>Meanwhile, an associative Bash array looks like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">declare -a IndexedArray\nAssociativeArray[color]=blue\nAssociativeArray[type]=car\nAssociativeArray[topspeed]=200<\/pre><p>You can also simplify arrays by writing them in a single line using indices, like in these examples:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IndexedArray=([0]='car' [1]='plane' [2]='bike')<\/pre><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">declare -A Associative Array=( [color]=blue [type]=car [topspeed]=200 )<\/pre><p>Note that the first example encloses the array elements in quotation marks. This is optional, but becomes necessary when the items are space-separated strings or an empty string. <\/p><p>For indexed arrays specifically, you can omit the subscript when writing it in a single line because Bash automatically assigns the number at a fixed increment:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IndexedArray=(car plane bike)<\/pre><p>Remember that indexing starts at <strong>0<\/strong>, so the above example will assign the <strong>car <\/strong>element of the array to the <strong>0<\/strong> index.<\/p><p>Another interesting feature of bash arrays is that following index numbers in order is not necessary. For example, you can declare only the first and third elements while leaving the second element of an array empty:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IndexedArray[0]=car\nIndexedArray[2]=bike<\/pre><p>Now that we know how to declare an array, let&rsquo;s learn about different operations for Bash array manipulation.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-do-you-add-a-variable-to-a-bash-array\">How do you add a variable to a Bash array?<\/h2><p>Adding <a href=\"\/ca\/tutorials\/bash-variables\">Bash variables<\/a> means inserting a new element to your array using the <strong>+=<\/strong> operator. The syntax looks like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ArrayName+=(element-to-add)<\/pre><p>For example, here&rsquo;s how to add a new item to an indexed array:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IndexedArray=(car plane bike)\nIndexedArray+=(motorcycle)<\/pre><p>The indexed array has a new element now. Remember that this method appends to the end of an array. Therefore, the <strong>motorcycle<\/strong> element will be added as the last element.<\/p><p>For associative arrays, the process is very similar, except you need to specify the keys along with all the elements:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">declare -A AssociativeArray\nAssociativeArray[color]=blue\nAssociativeArray+=([tires]=alloy [engine]=gasoline)<\/pre><p>Adding a variable is an essential operation for expanding your Bash array with new data. It is commonly used for tasks like storing command outputs and logging events on your system. <\/p><p><div class=\"protip\">\n                    <h4 class=\"title\">Using Bash operator for another task<\/h4>\n                    <p>In addition to appending an array with a new item, the <strong>+=<\/strong> operator lets you <a href=\"\/ca\/tutorials\/bash-concatenate-strings\">concatenate strings in your Bash script<\/a>.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-do-you-reference-and-print-a-bash-array-element\">How do you reference and print a Bash array element?<\/h2><p>Referring to and printing a Bash array element lets you check an element associated with a specific index or key. To do this, create an indexed array:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IndexedArray=(car plane bike)<\/pre><p>To reference the first array variable, use the following syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">${IndexedArray[0]}<\/pre><p>Combine it with <strong>echo<\/strong>, and you will get the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo ${IndexedArray[0]}<\/pre><p>The output will show you the first element. In this case, it&rsquo;s <strong>car<\/strong>. The same logic applies when referencing and printing an associative array:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">declare -A AssociativeArray=( [color]=blue [type]=car [topspeed]=200 )\necho ${AssociativeArray[type]}<\/pre><p>The output will be <strong>car <\/strong>as well.<\/p><p>To print the whole array, use <strong>@ <\/strong>as an index. The full script looks like this:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1da62a89a0\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"579\" height=\"218\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Bash-script-to-create-and-print-out-an-indexed-array-1.png\" alt=\"Bash script to create and print out an indexed array\" class=\"wp-image-54629\"  sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>You can also print the keys of an array instead. To do this, add an exclamation mark before the array name:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1da62a9bf9\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"222\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Command-line-script-to-print-out-all-the-keys-of-an-associative-array.-To-print-all-the-values-instead-only-the-exclamation-point-would-need-to-be-removed.png\" alt=\"Command-line script to print out all the keys of an associative array. To print all the values instead, only the exclamation point would need to be removed\" class=\"wp-image-54630\"  sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Printing an element of an array is helpful for checking your script before passing it through other commands or functions, ensuring you work with the correct data. <\/p><h2 class=\"wp-block-heading\" id=\"h-how-do-you-remove-bash-array-elements\">How do you remove Bash array elements?<\/h2><p>Deleting array elements removes a specific item from your array, using a process similar to referencing them. Use an index or a key combined with the <strong>unset <\/strong>builtin to delete an array element, as shown in this syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">unset ArrayName[index-or-key]<\/pre><p>Here&rsquo;s an example of deleting a single element from an indexed array:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1da62ac125\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"425\" height=\"217\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Bash-script-to-show-unset-command-which-is-used-to-delete-elements-from-an-array.png\" alt=\"Bash script to show unset command which is used to delete elements from an array\" class=\"wp-image-54631\"  sizes=\"auto, (max-width: 425px) 100vw, 425px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>A similar logic applies to associative arrays:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1da62ad454\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"655\" height=\"191\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Bash-script-showcasing-unset-command-for-associative-arrays-which-is-used-to-remove-an-element-from-the-array.png\" alt=\"Bash script showcasing unset command for associative arrays, which is used to remove an element from the array\" class=\"wp-image-54632\"  sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>To delete an entire array, specify <strong>unset<\/strong> with the array name as shown here:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1da62ae64a\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"657\" height=\"207\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Shell-script-to-delete-array-completely.png\" alt=\"Shell script to delete array completely\" class=\"wp-image-54633\"  sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Nothing is shown after trying to print the array elements because the <strong>unset<\/strong> builtin deleted them. Also, this will completely remove the index from your array. For example, if you unset <strong>1 <\/strong>from the following array, only <strong>0<\/strong> and <strong>2<\/strong> remain:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IndexedArray=([0]='car' [1]='plane' [2]='bike')<\/pre><p>If you unset an index or key that doesn&rsquo;t exist, your array won&rsquo;t change. You also won&rsquo;t get an error and the operation will complete silently. <\/p><p>Removing an item from your Bash arrays is common if you store dynamic data like logs. As your script expands, you may want to delete unused elements to make it more organized. <\/p><h2 class=\"wp-block-heading\" id=\"h-how-do-you-loop-through-a-bash-array\">How do you loop through a Bash array?<\/h2><p><a href=\"\/ca\/tutorials\/bash-for-loop-guide-and-examples\/\">Creating Bash loops<\/a> is a fundamental aspect of learning Bash scripting basics. You can use loops with arrays as well, using this syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i in \"${Array[@]}\"; do action; done<\/pre><p>For example, the most common use case is to iterate over each array item:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1da62b02bc\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"659\" height=\"231\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Shell-script-with-a-for-loop-to-iterate-and-print-an-array-e1652628178838.png\" alt=\"Shell script with a for loop to iterate and print an array\" class=\"wp-image-54634\"><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>You can also combine keys with the array elements and print them all together like this:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1da62b171d\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"797\" height=\"234\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Command-line-script-to-print-out-both-elements-and-keys-for-an-associative-array.png\" alt=\"Command-line script to print out both elements and keys for an associative array\" class=\"wp-image-54635\"  sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Looping through an array is helpful for automating tasks involving a lot of data. For example, you can batch rename files by passing them through the <strong>mv <\/strong>command, or install multiple packages simultaneously using your system&rsquo;s package manager. <\/p><h2 class=\"wp-block-heading\" id=\"h-how-do-you-pass-a-bash-array-to-a-function\">How do you pass a Bash array to a function?<\/h2><p>Passing a Bash array to functions saves a considerable amount of time when scripting. Instead of writing the same code repeatedly, you can call out an already-written function. We will combine the previously mentioned iteration loop and make a function out of it:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Iteration\n{\nm=${#IndexedArray[@]}\nfor (( i=0; i&lt;m; i++ ))\ndo\necho ${IndexedArray[$i]}\ndone\n}\nIndexedArray=(car bike plane)\nIteration ${IndexedArray[@]}<\/pre><p>Running it on the command line will get you the following result:<\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1da62b2937\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"477\" height=\"417\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Bash-function-for-array-iteration.png\" alt=\"Bash function for array iteration\" class=\"wp-image-54636\"  sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Incorporating functions when using Bash arrays in scripting helps you complete complex tasks involving a large dataset, like parsing outputs or filtering through logs. This operation typically works together with a loop to iterate through the data. <\/p><h2 class=\"wp-block-heading\" id=\"h-key-takeaways\">Key takeaways<\/h2><p>Bash array enables you to work with a large amount of data more efficiently, which is helpful when managing a UNIX-like system like a Linux VPS. In real-world applications, Bash arrays are commonly used to pass values into commands, functions, or loops.&nbsp;<\/p><p>Since the use of Bash arrays with functions, loops, and commands varies depending on the task, we highly recommend exploring them in more detail. If you need references, check out our <a href=\"\/ca\/tutorials\/bash-script-example\">Bash script examples tutorial<\/a> to learn more about how these scripts might look in real-world applications. <\/p><p>To deepen your understanding of Bash arrays&rsquo; applications, try creating a script that indexes and loops through files to perform an automated task like renaming. You can do this on your own Linux computer or set up a learning environment on <a href=\"\/ca\/vps-hosting\">Hostinger&rsquo;s VPS solution<\/a>.&nbsp;<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/ca\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>A Bash array is a structure in the Bash programming language that lets you group and index items, which can be strings or numbers. It helps you work with a lot of data more efficiently, streamlining various UNIX system administration tasks like storing command outputs. You can write Bash arrays in two ways: indexed and [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ca\/tutorials\/how-to-use-bash-array\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":279,"featured_media":142015,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"Bash array tutorial: How to use arrays in bash","rank_math_description":"A Bash array is a structure used to store items with indexes for better organization. Learn its syntax and its real-world applications here.","rank_math_focus_keyword":"bash array","footnotes":""},"categories":[22699],"tags":[],"class_list":["post-54623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-use-bash-array","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-usar-bash-arrays","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-usar-bash-arrays","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-use-bash-array","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-use-bash-array","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-use-bash-array","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-use-bash-array","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-use-bash-array","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-use-bash-array","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-use-bash-array","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/54623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/users\/279"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/comments?post=54623"}],"version-history":[{"count":27,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/54623\/revisions"}],"predecessor-version":[{"id":142014,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/54623\/revisions\/142014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media\/142015"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media?parent=54623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/categories?post=54623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/tags?post=54623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}