{"id":54623,"date":"2022-05-13T15:09:26","date_gmt":"2022-05-13T15:09:26","guid":{"rendered":"\/tutorials\/?p=54623"},"modified":"2025-04-23T10:09:55","modified_gmt":"2025-04-23T10:09:55","slug":"bash-array","status":"publish","type":"post","link":"\/uk\/tutorials\/bash-array","title":{"rendered":"How to Use Bash Arrays"},"content":{"rendered":"<p>Bash is the go-to choice for automation for Linux users. Since automation often deals with thousands of elements, it&rsquo;s essential to know what a bash array is.<\/p><p>With bash arrays, managing VPS or physical servers is much easier. This tutorial will show you different types of bash arrays and provide useful examples.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet-1024x283.png\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-1536x425.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-300x83.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-150x41.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet-768x212.png 768w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/11\/Linux-cheat-sheet.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>\n\n\n\n\n<div class=\"protip\">\n                    <h2 class=\"featured-snippet title\">What Is a Bash Array?<\/h2>\n                    <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, bash arrays can store different types of elements. For example, you can use a bash array to store both strings and numbers.<\/p>\n                <\/div>\n\n\n\n<\/p><p>There are two types of bash arrays:<\/p><ul class=\"wp-block-list\">\n<li><strong>Indexed<\/strong> &ndash; the array is referred via integers or numbers.<\/li>\n\n\n\n<li><strong>Associative<\/strong> &ndash; the array is referred via strings or a set of characters and words.<\/li>\n<\/ul><p>Remember that bash does not support multidimensional arrays, so it&rsquo;s not possible to add an array within an array.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-declare-array-in-bash\">How to Declare Array in Bash<\/h2><p>There are a few ways to declare indexed and associative arrays in bash. It&rsquo;s worth noting that the bash array size does not need to be declared beforehand because bash arrays have no upper limit on the number of elements they can store.<\/p><h3 class=\"wp-block-heading\" id=\"h-indexed-array\">Indexed Array<\/h3><p>We will start with a simple bash indexed array. For example, we&rsquo;ll use it to create a list of different means of transportation.<\/p><p>The first option is to declare an array by using the shell builtin <strong>declare<\/strong> with the <strong>-a<\/strong> flag and give the array its 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 IndexedArray\nIndexedArray[0]=car\nIndexedArray[1]=plane\nIndexedArray[2]=bike<\/pre><p>The same can be achieved without the <strong>declare <\/strong>builtin:<\/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[1]=plane\nIndexedArray[2]=bike<\/pre><p>Or, make it even simpler by going with:<\/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>However, there is an option to set an array with indices:<\/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]=&rsquo;car&rsquo; [1]=&rsquo;plane&rsquo; [2]=&rsquo;bike&rsquo;)<\/pre><p>An 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><h3 class=\"wp-block-heading\" id=\"h-associative-array\">Associative Array<\/h3><p>While indexed arrays don&rsquo;t require the <strong>declare<\/strong> builtin, it won&rsquo;t be possible to create an associative bash array without declaring it first:<\/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<\/pre><p>Next, add the values. Keep in mind that the key must be a string:<\/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=\"\">AssociativeArray[color]=blue\nAssociativeArray[type]=car\nAssociativeArray[topspeed]=200<\/pre><p>An alternative way would be:<\/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 )<\/pre><h2 class=\"wp-block-heading\" id=\"h-how-to-add-a-variable-to-a-bash-array\">How to Add a Variable to a Bash Array<\/h2><p>Easily add <a href=\"\/uk\/tutorials\/bash-variables\">bash variables<\/a> using the <strong>+=<\/strong> operator. For example, the process for an indexed array would look 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=\"\">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><h2 class=\"wp-block-heading\" id=\"h-how-to-reference-and-print-an-array-element\">How to Reference and Print an Array Element<\/h2><p>Users can reference bash array values using the element 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\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Bash-script-to-create-and-print-out-an-indexed-array-1.png\"><img decoding=\"async\" width=\"579\" height=\"218\" 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\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-to-create-and-print-out-an-indexed-array-1.png 579w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-to-create-and-print-out-an-indexed-array-1-300x113.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-to-create-and-print-out-an-indexed-array-1-150x56.png 150w\" sizes=\"(max-width: 579px) 100vw, 579px\" \/><\/a><\/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\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/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\"><img decoding=\"async\" width=\"662\" height=\"222\" 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\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/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 662w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/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-300x101.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/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-150x50.png 150w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-how-to-remove-bash-array-elements\">How to Remove Bash Array Elements<\/h2><p>Deleting array elements is similar to referencing them. Use an index or a key combined with the <strong>unset <\/strong>builtin to delete an array element.<\/p><p>Here&rsquo;s an example of deleting a single element from an indexed array:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Bash-script-to-show-unset-command-which-is-used-to-delete-elements-from-an-array.png\"><img decoding=\"async\" width=\"425\" height=\"217\" 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\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-to-show-unset-command-which-is-used-to-delete-elements-from-an-array.png 425w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-to-show-unset-command-which-is-used-to-delete-elements-from-an-array-300x153.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-to-show-unset-command-which-is-used-to-delete-elements-from-an-array-150x77.png 150w\" sizes=\"(max-width: 425px) 100vw, 425px\" \/><\/a><\/figure><\/div><p>A similar logic applies to associative arrays:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/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\"><img decoding=\"async\" width=\"655\" height=\"191\" 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\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-showcasing-unset-command-for-associative-arrays-which-is-used-to-remove-an-element-from-the-array.png 655w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-showcasing-unset-command-for-associative-arrays-which-is-used-to-remove-an-element-from-the-array-300x87.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-script-showcasing-unset-command-for-associative-arrays-which-is-used-to-remove-an-element-from-the-array-150x44.png 150w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><\/a><\/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\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Shell-script-to-delete-array-completely.png\"><img decoding=\"async\" width=\"657\" height=\"207\" 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\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Shell-script-to-delete-array-completely.png 657w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Shell-script-to-delete-array-completely-300x95.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Shell-script-to-delete-array-completely-150x47.png 150w\" sizes=\"(max-width: 657px) 100vw, 657px\" \/><\/a><\/figure><\/div><p>Nothing is shown after trying to print the array elements because the <strong>unset<\/strong> builtin deleted them.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-loop-through-an-array\">How to Loop Through an Array<\/h2><p><a href=\"\/uk\/tutorials\/bash-for-loop-guide-and-examples\/\">Creating bash loops<\/a> is a fundamental aspect of learning <a href=\"\/uk\/tutorials\/bash-scripting-tutorial\">bash scripting basics<\/a>. You can use loops with arrays as well. For example, the most common use case is to iterate over each array item:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Shell-script-with-a-for-loop-to-iterate-and-print-an-array-e1652628178838.png\"><img decoding=\"async\" width=\"659\" height=\"231\" 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\"><\/a><\/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\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Command-line-script-to-print-out-both-elements-and-keys-for-an-associative-array.png\"><img decoding=\"async\" width=\"797\" height=\"234\" 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\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Command-line-script-to-print-out-both-elements-and-keys-for-an-associative-array.png 797w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Command-line-script-to-print-out-both-elements-and-keys-for-an-associative-array-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Command-line-script-to-print-out-both-elements-and-keys-for-an-associative-array-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Command-line-script-to-print-out-both-elements-and-keys-for-an-associative-array-768x225.png 768w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-how-to-pass-an-array-to-a-function\">How to Pass an Array to a Function<\/h2><p>Functions save 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\">\n<figure class=\"aligncenter size-full\"><a href=\"\/uk\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/05\/Bash-function-for-array-iteration.png\"><img decoding=\"async\" width=\"477\" height=\"417\" 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\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-function-for-array-iteration.png 477w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-function-for-array-iteration-300x262.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2022\/05\/Bash-function-for-array-iteration-150x131.png 150w\" sizes=\"(max-width: 477px) 100vw, 477px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Bash is one of the most popular shells and command languages for virtual servers as well as physical Linux-based servers. With bash scripting and arrays, users can automate their work and save hundreds of hours manually doing tasks.<\/p><p>In this tutorial, we&rsquo;ve covered the majority of array operations:<\/p><ul class=\"wp-block-list\">\n<li>Declaring and creating indexed and associative arrays.<\/li>\n\n\n\n<li>Adding and removing variables from arrays.<\/li>\n\n\n\n<li>Referencing and printing arrays.<\/li>\n\n\n\n<li>Looping through arrays and passing them to functions.<\/li>\n<\/ul><p>We also provided some examples that you can use when tinkering around with bash. If you have any questions or comments, leave them below.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/uk\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img 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\" srcset=\"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/uk\/tutorials\/wp-content\/uploads\/sites\/51\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-how-to-use-bash-arrays-faq\">How to Use Bash Arrays FAQ<\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1652628235890\"><h3 class=\"schema-faq-question\">Bash Array vs String<\/h3> <p class=\"schema-faq-answer\">A bash array stores a set of elements &ndash; for example, a set of numbers along with a list of words. On the other hand, a string can be considered an array, but it can only store characters and nothing else.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1652628248417\"><h3 class=\"schema-faq-question\">Bash Array vs List<\/h3> <p class=\"schema-faq-answer\">A bash list is a sequence of one or more pipelines separated by one of the operators. As such, lists are not related to arrays. However, indexed arrays are sometimes referred to as lists.&nbsp;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1652628260519\"><h3 class=\"schema-faq-question\">What Do {} Mean in Bash?<\/h3> <p class=\"schema-faq-answer\">Curly braces without a $ sign are considered brace expansions and are used to create arbitrary strings. You can use braces to build arrays. For example, <strong>echo {0..100}<\/strong> will print numbers from zero to 100.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1652628270632\"><h3 class=\"schema-faq-question\">Is Bash a Language?<\/h3> <p class=\"schema-faq-answer\">Yes, bash is a command language. It is used to either automate tasks or run commands on the command line. Compared to most programming languages, the bash command language is easier to learn, and the syntax is relatively straightforward.&nbsp;&nbsp;<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1652628289869\"><h3 class=\"schema-faq-question\">How to Echo a Bash Array?<\/h3> <p class=\"schema-faq-answer\">To echo an array, use the format <strong>echo ${Array[0]}<\/strong>. <strong>Array<\/strong> is your array name, and <strong>0<\/strong> is the index or the key if you are echoing an associative array. You can also use <strong>@<\/strong> or <strong>*<\/strong> symbols instead of an index to print the entire array.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash is the go-to choice for automation for Linux users. Since automation often deals with thousands of elements, it&rsquo;s essential to know what a bash array is. With bash arrays, managing VPS or physical servers is much easier. This tutorial will show you different types of bash arrays and provide useful examples. There are two [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/uk\/tutorials\/bash-array\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":279,"featured_media":76455,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"Learning Bash Arrays - A Beginner\u2019s Guide","rank_math_description":"A bash array is a variable containing a few elements or strings. Read on to learn what they are and how to work with them. Examples included.","rank_math_focus_keyword":"bash array","footnotes":""},"categories":[22644,22640],"tags":[],"class_list":["post-54623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/54623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/users\/279"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/comments?post=54623"}],"version-history":[{"count":19,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/54623\/revisions"}],"predecessor-version":[{"id":124006,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/posts\/54623\/revisions\/124006"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media\/76455"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/media?parent=54623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/categories?post=54623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-json\/wp\/v2\/tags?post=54623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}