{"id":23449,"date":"2023-10-01T11:17:10","date_gmt":"2023-10-01T11:17:10","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=23449"},"modified":"2023-12-31T02:27:14","modified_gmt":"2023-12-31T02:27:14","slug":"bash-array-expansion","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/","title":{"rendered":"What is Array Expansion in Bash [4 Useful Applications]"},"content":{"rendered":"<p style=\"text-align: justify;\"><strong>Bash<\/strong> shell offers multiple array handling tools and techniques. Among them array expansion is prominent. It takes the array handling capabilities to the next level. In this article, I want to demonstrate the basic syntax of array expansion as well as various practical uses of it in Bash.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Practice-Files_bash-array-expansion.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files for Array Expansion in Bash<\/span><\/a><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#Bash_Array_Fundamentals\" >Bash Array Fundamentals<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#%E2%97%8F_Index_Array\" >\u25cf Index Array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#%E2%97%8F_Associate_Array\" >\u25cf Associate Array<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#Syntax_of_Array_Expansion_in_Bash\" >Syntax of Array Expansion in Bash<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#Difference_Between_%E2%80%9C%E2%80%9D_and_%E2%80%9C%E2%80%9D_in_Array_Expansion\" >Difference Between \u201c[@]\u201d and \u201c[*]\u201d in Array Expansion<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#4_Useful_Applications_of_Bash_Array_Expansion\" >4 Useful Applications of Bash Array Expansion<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#1_Slicing_Array_to_Extract_Range_of_Elements\" >1. Slicing Array to Extract Range of Elements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#2_Combining_Multiple_Arrays_Into_One_Using_Array_Expansion\" >2. Combining Multiple Arrays Into One Using Array Expansion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#3_Counting_Number_of_Elements_in_an_Array_Using_Expansion_in_Bash\" >3. Counting Number of Elements in an Array Using Expansion in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#4_Match_Patterns_and_Replace_Elements_in_Array\" >4. Match Patterns and Replace Elements in Array<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#Array_Creation_Using_Array_Expansion_in_Bash\" >Array Creation Using Array Expansion in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#How_to_count_array_in_Bash\" >How to count array in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#Why_expanded_into_multiple_words_in_array\" >Why @ expanded into multiple words in array?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#What_is_the_maximum_size_of_a_Bash_array\" >What is the maximum size of a Bash array?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#What_is_the_best_way_to_declare_elements_with_whitespaces_in_an_array\" >What is the best way to declare elements with whitespaces in an array?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Bash_Array_Fundamentals\"><\/span>Bash Array Fundamentals<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">An array is a collection of elements. It is a structured way to organize and access data. There are two types of arrays in <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/introduction\/\" target=\"_blank\" rel=\"noopener\"><strong>Bash<\/strong><\/a>. Index array and associate array.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E2%97%8F_Index_Array\"><\/span>\u25cf Index Array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">Indexed arrays are the most common type of arrays in Bash and are similar to arrays in many other programming languages. There are a couple of ways to declare an index array in Bash. First of all, you can initiate an indexed array using the <code class=\"\" data-line=\"\">declare<\/code> with <code class=\"\" data-line=\"\">-a <\/code>option.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">declare -a array1=(&quot;a&quot; &quot;b&quot; &quot;c&quot;)<\/code><\/pre>\n<p style=\"text-align: justify;\">However, it is not mandatory to use <strong>declare<\/strong> explicitly to initiate an array. Rather one can use the following structure to initiate an array quickly.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">array2=(&quot;d&quot; &quot;e&quot; &quot;f&quot;)<\/code><\/pre>\n<p style=\"text-align: justify;\">To access an element of an array put the index of the element within a square bracket after the name of the array. For instance, to get the second element of <strong>array2<\/strong>, use <code class=\"\" data-line=\"\">array2[1]<\/code>\u00a0in <strong>parameter expansion<\/strong>.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;${array2[1]}&quot; # prints &quot;e&quot;<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"%E2%97%8F_Associate_Array\"><\/span>\u25cf Associate Array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">Unlike an indexed array, an associate array is a collection of data with a key associated with each element of the array. To access an element of an associate array, one needs to use the key associated with that element. This is similar to the <strong>key-value<\/strong> pair of a Python dictionary.<\/p>\n<p style=\"text-align: justify;\">You have to use the <code class=\"\" data-line=\"\">declare <\/code>keyword with <code class=\"\" data-line=\"\">-A<\/code> to initiate an associate array. You can define the full array in a single line like below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">declare -A array3 ( [User]=Anita [ID]=20134 [Shell]=sh )<\/code><\/pre>\n<p style=\"text-align: justify;\">Furthermore, you can first declare an associate array and later add an element in that array.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">declare -A array4\n[User]=Kelly\n[ID]=20129\n[Shell]=zsh<\/code><\/pre>\n<p style=\"text-align: justify;\">To access an element of an associate array put the <strong>key<\/strong> of the element within a square bracket after the name of the array. For instance, to get the value of the <strong>User<\/strong> key of <strong>array4<\/strong>, use <code class=\"\" data-line=\"\">array4[User]<\/code>\u00a0in <strong>parameter expansion<\/strong>.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;${array4[User]}&quot; # prints &quot;Kelly&quot;<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Syntax_of_Array_Expansion_in_Bash\"><\/span>Syntax of Array Expansion in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Array expansion <\/strong>in Bash is a kind of parameter expansion that typically refers to the process of expanding the elements of an array. \u201c<strong>[@]<\/strong>\u201d and \u201c<strong>[*]<\/strong>\u201d syntax can be used for expanding array. Both indexed and associate arrays can be expanded in this way. Let\u2019s define a new array and explore array expansion using the aforementioned syntax.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">my_arr=(&quot;a b&quot; &quot;1 2&quot;)<\/code><\/pre>\n<p style=\"text-align: justify;\"><code class=\"\" data-line=\"\">${my_arr[@]}<\/code>\u00a0will expand each element of the array as a separate word. Here <strong>my_arr <\/strong>has two elements &#8220;a b&#8221; and &#8220;1 2&#8221; respectively. However, there are four separate words in these two elements as you the see in the following image.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo ${my_arr[@]}\n# Output: a b 1 2<\/code><\/pre>\n<p style=\"text-align: justify;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-23451\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/1.-Syntax-of-array-expansion.png\" alt=\"Syntax of array expansion\" width=\"599\" height=\"170\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/1.-Syntax-of-array-expansion.png 599w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/1.-Syntax-of-array-expansion-300x85.png 300w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><code class=\"\" data-line=\"\">${my_arr[*]}<\/code>\u00a0also expands each element of the array as a separate word and results in four different words. But there is a <strong>slight difference <\/strong>between <strong>[@]<\/strong>\u00a0 and <strong>[*]<\/strong>\u00a0that I will discuss later in this article.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo ${my_arr[*]}\n# Output: a b 1 2<\/code><\/pre>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-23453\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/2.-Expanding-an-array-as-single-string.png\" alt=\"Expanding an array as single string\" width=\"582\" height=\"155\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/2.-Expanding-an-array-as-single-string.png 582w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/2.-Expanding-an-array-as-single-string-300x80.png 300w\" sizes=\"(max-width: 582px) 100vw, 582px\" \/><div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#E8F9FC;border-color:#ffffff;color:#333333;\"><strong>NOTE:<\/strong> <strong>${!array[@]} <\/strong>or<strong> ${!array[*]} <\/strong>provides the index for an indexed array and the key for an associative array, respectively <\/div><\/div>\n<div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#E8F9FC;border-color:#ffffff;color:#333333;\"><strong>NOTE:<\/strong> The command <strong>unset arr[@]<\/strong>\u00a0will unset or remove all elements from an array named <strong>arr<\/strong>.<\/div><\/div>\n<h4><span class=\"ez-toc-section\" id=\"Difference_Between_%E2%80%9C%E2%80%9D_and_%E2%80%9C%E2%80%9D_in_Array_Expansion\"><\/span>Difference Between \u201c[@]\u201d and \u201c[*]\u201d in Array Expansion<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">You can\u2019t differentiate between <strong>[@]<\/strong> and <strong>[*]<\/strong> unless you use these with quotes. In this demonstration, I will show you the basic difference between these two using a for loop. Let&#8217;s see the bash script to understand the difference between <code class=\"\" data-line=\"\">[@]<\/code> and <code class=\"\" data-line=\"\">[*]<\/code> in array expansion using <code class=\"\" data-line=\"\">for loop<\/code>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nmy_arr=(&quot;a b&quot; &quot;1 2&quot;)\n# Using [@] for array expansion\necho &quot;Using [@]:&quot;\nfor item in &quot;${my_arr[@]}&quot;; do\necho &quot;$item&quot;\ndone\n\n# Using [*] for array expansion\necho &quot;Using [*]:&quot;\nfor item in &quot;${my_arr[*]}&quot;; do\necho &quot;$item&quot;\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The script initializes an array called <strong>my_arr<\/strong> with two elements, &#8220;a b&#8221; and &#8220;1 2&#8221;. Then it demonstrates the difference between array expansion using <code class=\"\" data-line=\"\">[@]<\/code> and <code class=\"\" data-line=\"\">[*]<\/code> within two separate for loops. When using <code class=\"\" data-line=\"\">${my_arr[@]}<\/code>, each element of the array is treated as a separate word.<\/p>\n<p>When using <code class=\"\" data-line=\"\">${my_arr[*]}<\/code>, the entire array is treated as a single string causing array elements to be printed on a single line.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-23456\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/5.-Difference-between-two-types-of-array-expansion.png\" alt=\"Difference between two types of array expansion\" width=\"524\" height=\"250\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/5.-Difference-between-two-types-of-array-expansion.png 524w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/5.-Difference-between-two-types-of-array-expansion-300x143.png 300w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/>When <strong>${my_arr[@]}<\/strong>\u00a0is used for array expansion, the loop iterates over the array (&#8220;a b&#8221; &#8220;1 2&#8221;) for <strong>two<\/strong> iterations. It prints elements <strong>a b <\/strong>and <strong>1 2<\/strong>\u00a0in new line.<\/p>\n<p style=\"text-align: justify;\">On the contrary, when <strong>${my_arr[]}<\/strong>\u00a0is used, the loop iterates over the array only once, treating the entire array as a single entity and thus resulting in <strong>a b 1 2<\/strong>\u00a0as a single string.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Useful_Applications_of_Bash_Array_Expansion\"><\/span>4 Useful Applications of Bash Array Expansion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Array expansion serves many purposes such as slicing an array, combining multiple arrays, and counting elements within an array. It proves to be a valuable tool in various applications in Bash scripting.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Slicing_Array_to_Extract_Range_of_Elements\"><\/span>1. Slicing Array to Extract Range of Elements<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Array slicing is one of the major operations performed on an array. The following script will show you the way to slice an array or extract a subarray from an array. Here&#8217;s a bash script to extract a range of elements by <strong>array slicing<\/strong>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Create an array of numbers\nmyArray=(10 20 30 40 50 60 70)\n\n# Slice the array to extract elements from index 2 to 4 (inclusive)\nslicedArray=(&quot;${myArray[@]:2:3}&quot;)\n\n# Print the slicedArray\necho &quot;${slicedArray[@]}&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>This Bash script initializes an array called <strong>myArray<\/strong>. It then slices the array from index <strong>2<\/strong> (the third element in <strong>myArray<\/strong>) and includes the next <strong>3<\/strong> elements, resulting in <strong>slicedArray<\/strong> containing the values <strong>30<\/strong>, <strong>40<\/strong>, and <strong>50<\/strong>.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23457\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/6.-Slicing-an-array.png\" alt=\"Slicing an array\" width=\"534\" height=\"133\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/6.-Slicing-an-array.png 534w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/6.-Slicing-an-array-300x75.png 300w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/>The program slices the array <strong>(10 20 30 40 50 60 70)<\/strong> from index <strong>2 <\/strong>up to the next <strong>3<\/strong> items of the array.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Combining_Multiple_Arrays_Into_One_Using_Array_Expansion\"><\/span>2. Combining Multiple Arrays Into One Using Array Expansion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">One can easily combine two or more arrays into a new array using Bash array expansion. <code class=\"\" data-line=\"\">[@]<\/code>\u00a0is used for expanding the arrays and <code class=\"\" data-line=\"\">+<\/code> operator is used to add them. Check the bash script to combine multiple arrays into one using <strong>bash array expansion<\/strong>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\narr1=(a b c d)\narr2=(1 2 3)\ncombined=(&quot;${arr1[@]}&quot; &quot;${arr2[@]}&quot;)\u00a0 # Use arr2 here\necho &quot;${combined[@]}&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>This Bash script defines two arrays, <strong>arr1<\/strong> and <strong>arr2<\/strong>. It then combines these arrays into a new array called <strong>combined <\/strong>using <strong>array expansion<\/strong>. The <strong>combined<\/strong> array contains all the elements from <strong>arr1<\/strong> followed by all the elements from <strong>arr2<\/strong>. Finally, it prints the elements of the <strong>combined<\/strong> array using the <code class=\"\" data-line=\"\">echo<\/code> command.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23458\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/7.-Combining-two-array-into-one.png\" alt=\"Combining two array into one\" width=\"566\" height=\"125\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/7.-Combining-two-array-into-one.png 566w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/7.-Combining-two-array-into-one-300x66.png 300w\" sizes=\"(max-width: 566px) 100vw, 566px\" \/>Here, <strong>arr1 <\/strong>contains the elements <strong>a<\/strong>, <strong>b<\/strong> and <strong>c<\/strong>, and <strong>arr2<\/strong> contains the numbers <strong>1<\/strong>, <strong>2<\/strong>, and <strong>3<\/strong>. The program expands both arrays and combines them. The resulting <strong>combined <\/strong>array <strong>a b c d 1 2 3<\/strong>\u00a0is displayed using the <strong>echo <\/strong>command.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Counting_Number_of_Elements_in_an_Array_Using_Expansion_in_Bash\"><\/span>3. Counting Number of Elements in an Array Using Expansion in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Array expansion is necessary to count the total number of elements in an array. Place a <code class=\"\" data-line=\"\">#<\/code> to count the length of an array or element number before expanding the array using the <code class=\"\" data-line=\"\">[@]<\/code> syntax. Follow the bash script for counting array elements:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\narr=(&quot;jpg&quot; &quot;png&quot; &quot;doc&quot; &quot;sh&quot; &quot;txt&quot;)\ncount=&quot;${#arr[@]}&quot;\necho &quot;The number of elements in the array is: $count&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>This Bash script initializes an array called <strong>arr<\/strong>. It then uses the <code class=\"\" data-line=\"\">${#arr[@]}<\/code> syntax to determine the number of elements in the array. Finally, it prints the number of elements in the array using the <code class=\"\" data-line=\"\">echo<\/code> command.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23459\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/8.-Finding-length-of-the-array-using-array-expansion-in-Bash.png\" alt=\"Finding length of the array using array expansion in Bash\" width=\"700\" height=\"125\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/8.-Finding-length-of-the-array-using-array-expansion-in-Bash.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/8.-Finding-length-of-the-array-using-array-expansion-in-Bash-300x54.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/>The <strong>arr<\/strong> array defined in the code has five elements &#8220;jpg&#8221; &#8220;png&#8221; &#8220;doc&#8221; &#8220;sh&#8221; and &#8220;txt&#8221; respectively. The program counts the number of elements of the arr as <strong>5 <\/strong>and prints it in the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Match_Patterns_and_Replace_Elements_in_Array\"><\/span>4. Match Patterns and Replace Elements in Array<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Array expansion can be used to find and replace elements within an array. You can iterate through the array to find a match or use the <strong>slash (\/)<\/strong> symbol after expansion to specify the word to find and replace.<\/p>\n<p style=\"text-align: justify;\">Take a look at the script below for a better understanding of how to match patterns and replace elements in array:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define an array\nmy_array=(10 20 Null 5 Null 25)\n\n# Use array expansion to perform the find and replace operation\nmy_array=(&quot;${my_array[@]\/Null\/0}&quot;)\n\n# Print the updated array\necho &quot;${my_array[@]}&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The script defines an array called <strong>my_array<\/strong> with integer values, including the string <strong>Null<\/strong>. It then uses array expansion to perform a find and replace operation, replacing all occurrences of <strong>Null<\/strong> with the integer <strong>0<\/strong>\u00a0within the array. Finally, it prints the updated array using the <code class=\"\" data-line=\"\">echo<\/code> command.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23460\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/9.-Replacing-using-array-expansion-in-Bash.png\" alt=\"Replacing using array expansion in Bash\" width=\"495\" height=\"131\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/9.-Replacing-using-array-expansion-in-Bash.png 495w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/9.-Replacing-using-array-expansion-in-Bash-300x79.png 300w\" sizes=\"(max-width: 495px) 100vw, 495px\" \/>The image shows that the program replaces <strong>Null<\/strong> with <strong>0<\/strong> in the <strong>(10 20 Null 5 Null 25)<\/strong>\u00a0array.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Array_Creation_Using_Array_Expansion_in_Bash\"><\/span>Array Creation Using Array Expansion in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Array expansion is useful to create and visualize an array. In this demonstration, I will use the <code class=\"\" data-line=\"\">mapfile<\/code> command to create an array from the contents of a file. Let&#8217;s see the bash script to learn how to create array with <strong>bash array expansion<\/strong>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nmapfile -t user &lt; &quot;users.txt&quot;\necho &quot;${user[@]}&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#E1E3E6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\"> EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The script reads the contents of the file named <strong>users.txt<\/strong>\u00a0into an array called <strong>user<\/strong>. The <code class=\"\" data-line=\"\">mapfile<\/code> command with the <code class=\"\" data-line=\"\">-t<\/code> option trims trailing newline characters from each line. This way it creates the <strong>user <\/strong>array by taking each line of <strong>users.txt<\/strong>\u00a0as separate elements in the array.<\/p>\n<\/div><\/div>\n<p>First, see the content of the <strong>users.txt <\/strong>file by running <code class=\"\" data-line=\"\">cat users.txt<\/code>:<\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23461\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/10.-Contents-of-a-file.png\" alt=\"Contents of a file\" width=\"504\" height=\"272\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/10.-Contents-of-a-file.png 504w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/10.-Contents-of-a-file-300x162.png 300w\" sizes=\"(max-width: 504px) 100vw, 504px\" \/>Now, running the script with <code class=\"\" data-line=\"\">.\/createarr.sh<\/code> command will output the below image of array creation:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-23462\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/11.-Creating-array-from-contents-of-a-file.png\" alt=\"Creating array from contents of a file\" width=\"534\" height=\"112\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/11.-Creating-array-from-contents-of-a-file.png 534w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/11.-Creating-array-from-contents-of-a-file-300x63.png 300w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In conclusion, array expansion is super useful for many useful applications. It is mandatory knowledge for slicing an array, replacing elements of an array and combining multiple arrays. I believe from now on you can perform all the above tasks and many more using array expansion in Bash.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"How_to_count_array_in_Bash\"><\/span>How to count array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To count the number of elements present in the bash array, use this syntax <code class=\"\" data-line=\"\">count=&quot;${#arr[@]}&quot;<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Why_expanded_into_multiple_words_in_array\"><\/span>Why @ expanded into multiple words in array?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><code class=\"\" data-line=\"\">[@]<\/code><strong>\u00a0\u00a0<\/strong>syntax expands all the words of an array rather than the elements only. Use quotes while expanding in order to expand the element only.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_maximum_size_of_a_Bash_array\"><\/span>What is the maximum size of a Bash array?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">There is no size limitation of a <strong>Bash<\/strong> array. Moreover, there is no limitation on the size of an element within the array.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_best_way_to_declare_elements_with_whitespaces_in_an_array\"><\/span>What is the best way to declare elements with whitespaces in an array?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The easiest way to add an element with whitespace is by specifying the index of the element. In case of an associative array use a new key to add an element with whitespace.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"How to count array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To count the number of elements present in the bash array, use this syntax count=\\\"${#arr[@]}\\\".\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Why @ expanded into multiple words in array?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"[@]\u00a0\u00a0syntax expands all the words of an array rather than the elements only. Use quotes while expanding in order to expand the element only.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the maximum size of a Bash array?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"There is no size limitation of a Bash array. Moreover, there is no limitation on the size of an element within the array.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the best way to declare elements with whitespaces in an array?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The easiest way to add an element with whitespace is by specifying the index of the element. In case of an associative array use a new key to add an element with whitespace.\"\n    }\n  }]\n}\n<\/script><br \/>\n<span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/brace-expansion\/\" target=\"_blank\" rel=\"noopener\">How to Use Brace Expansion in Bash Scripting [3 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/tilde-expansion\/\" target=\"_blank\" rel=\"noopener\">How to Use Tilde Expansion in Bash [4 Practical Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/parameter-expansion\/\" target=\"_blank\" rel=\"noopener\">Parameter Expansion in Bash [3 Main Types]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/arithmetic-expansion\/\" target=\"_blank\" rel=\"noopener\">Arithmetic Expansion in Bash [3 Practical Applications]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/history-expansion\/\" target=\"_blank\" rel=\"noopener\">An Exhaustive Guide to Bash History Expansion<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/glob-expansion\/\" target=\"_blank\" rel=\"noopener\">Glob Expansion in Bash<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;An Overview of Shell Expansion in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/\" target=\"_blank\" rel=\"noopener\">An Overview of Shell Expansion in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span>\u00a0<\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;23449&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;10&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4.9&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;4.9\\\/5 - (10 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;What is Array Expansion in Bash [4 Useful Applications]&quot;,&quot;width&quot;:&quot;139.6&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 139.6px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            4.9\/5 - (10 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash shell offers multiple array handling tools and techniques. Among them array expansion is prominent. It takes the array handling &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"What is Array Expansion in Bash [4 Useful Applications]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#more-23449\" aria-label=\"Read more about What is Array Expansion in Bash [4 Useful Applications]\">Read more<\/a><\/p>\n","protected":false},"author":314903,"featured_media":23463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[132],"class_list":["post-23449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-bash-expansion","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is Array Expansion in Bash [4 Useful Applications]<\/title>\n<meta name=\"description\" content=\"Array expansion is an essential technique in Bash to perform various array operation. This article talks about the topic in depth.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Array Expansion in Bash [4 Useful Applications]\" \/>\n<meta property=\"og:description\" content=\"Array expansion is an essential technique in Bash to perform various array operation. This article talks about the topic in depth.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/zahidlaku72\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-01T11:17:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-31T02:27:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Array-expansion-.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Md Zahidul Islam Laku\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Md Zahidul Islam Laku\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/\"},\"author\":{\"name\":\"Md Zahidul Islam Laku\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/26b37b2c7517d2c8702472bc3986ef21\"},\"headline\":\"What is Array Expansion in Bash [4 Useful Applications]\",\"datePublished\":\"2023-10-01T11:17:10+00:00\",\"dateModified\":\"2023-12-31T02:27:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/\"},\"wordCount\":1712,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Array-expansion-.png\",\"keywords\":[\"bash expansion\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/\",\"name\":\"What is Array Expansion in Bash [4 Useful Applications]\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Array-expansion-.png\",\"datePublished\":\"2023-10-01T11:17:10+00:00\",\"dateModified\":\"2023-12-31T02:27:14+00:00\",\"description\":\"Array expansion is an essential technique in Bash to perform various array operation. This article talks about the topic in depth.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Array-expansion-.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/Array-expansion-.png\",\"width\":400,\"height\":400,\"caption\":\"array expansion in Bash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/array-expansion\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"An Overview of Shell Expansion in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/expansion\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"What is Array Expansion in Bash [4 Useful Applications]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/26b37b2c7517d2c8702472bc3986ef21\",\"name\":\"Md Zahidul Islam Laku\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Zahidul-Islam-Laku-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Zahidul-Islam-Laku-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Zahidul-Islam-Laku-II-96x96.png\",\"caption\":\"Md Zahidul Islam Laku\"},\"description\":\"Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/zahidlaku72\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/zahidlaku\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCnxMIaAb-nXO5XnMeEaNhpQ\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/laku\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Array Expansion in Bash [4 Useful Applications]","description":"Array expansion is an essential technique in Bash to perform various array operation. This article talks about the topic in depth.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/","og_locale":"en_US","og_type":"article","og_title":"What is Array Expansion in Bash [4 Useful Applications]","og_description":"Array expansion is an essential technique in Bash to perform various array operation. This article talks about the topic in depth.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/zahidlaku72","article_published_time":"2023-10-01T11:17:10+00:00","article_modified_time":"2023-12-31T02:27:14+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Array-expansion-.png","type":"image\/png"}],"author":"Md Zahidul Islam Laku","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Md Zahidul Islam Laku","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/"},"author":{"name":"Md Zahidul Islam Laku","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/26b37b2c7517d2c8702472bc3986ef21"},"headline":"What is Array Expansion in Bash [4 Useful Applications]","datePublished":"2023-10-01T11:17:10+00:00","dateModified":"2023-12-31T02:27:14+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/"},"wordCount":1712,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Array-expansion-.png","keywords":["bash expansion"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/","name":"What is Array Expansion in Bash [4 Useful Applications]","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Array-expansion-.png","datePublished":"2023-10-01T11:17:10+00:00","dateModified":"2023-12-31T02:27:14+00:00","description":"Array expansion is an essential technique in Bash to perform various array operation. This article talks about the topic in depth.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Array-expansion-.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/10\/Array-expansion-.png","width":400,"height":400,"caption":"array expansion in Bash"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/array-expansion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"An Overview of Shell Expansion in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/"},{"@type":"ListItem","position":4,"name":"What is Array Expansion in Bash [4 Useful Applications]"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/26b37b2c7517d2c8702472bc3986ef21","name":"Md Zahidul Islam Laku","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Zahidul-Islam-Laku-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Zahidul-Islam-Laku-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Zahidul-Islam-Laku-II-96x96.png","caption":"Md Zahidul Islam Laku"},"description":"Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio","sameAs":["https:\/\/www.facebook.com\/zahidlaku72","https:\/\/www.linkedin.com\/in\/zahidlaku\/","https:\/\/www.youtube.com\/channel\/UCnxMIaAb-nXO5XnMeEaNhpQ"],"url":"https:\/\/linuxsimply.com\/author\/laku\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/23449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314903"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=23449"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/23449\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/23463"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=23449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=23449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=23449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}