{"id":37510,"date":"2024-02-22T08:48:02","date_gmt":"2024-02-22T08:48:02","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=37510"},"modified":"2024-03-31T06:17:22","modified_gmt":"2024-03-31T06:17:22","slug":"bash-string-array","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/","title":{"rendered":"How to Manipulate Bash String Array [5 Ways]"},"content":{"rendered":"<p style=\"text-align: justify;\">String stores user and system information. An array of strings stores a collection of strings in an array. For manipulating the Bash string array, follow the syntaxes:<\/p>\n<ol>\n<li>To declare an array of strings: <code class=\"\" data-line=\"\">array_name=(&quot;string1&quot; &quot;string2&quot; &quot;string3&quot; &quot;string4&quot;)<\/code><\/li>\n<li>To declare an associative array of strings: <code class=\"\" data-line=\"\">associative_array[&quot;key1&quot;]=&quot;string1&quot;<\/code><\/li>\n<li>To append an element to an array: <code class=\"\" data-line=\"\">array_name+=(&quot;new element&quot;)<\/code><\/li>\n<li>To remove an element from an array: <code class=\"\" data-line=\"\">unset cities[array_index]<\/code><\/li>\n<li>To sort an array alphabetically: <code class=\"\" data-line=\"\">sorted_array=($(for i in &quot;${array_name[@]}&quot;; do echo $i; done | sort))<\/code><\/li>\n<li>To convert comma-separated string to array: <code class=\"\" data-line=\"\">array_name=($(echo $string_name | tr &quot;,&quot; &quot;\\n&quot;))<\/code> Here, the comma will be replaced with a new line(\u201c\\n\u201d) character.<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">In the following article, I will discuss each of the aforementioned methods to manipulate the Bash string array in detail. So let\u2019s get started!<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-array-scripts.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 to Work with String Array 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' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#1_Declare_an_Array_of_Strings_in_Bash\" >1. Declare an Array of Strings in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#2_Loop_Through_String_Array_Indices\" >2. Loop Through String Array Indices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#3_Add_and_Remove_Elements_of_the_Array_of_String\" >3. Add and Remove Elements of the Array of String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#4_Sort_Arrays_of_String\" >4. Sort Arrays of String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#5_Split_String_to_Array\" >5. Split String to Array<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#_I_Using_%E2%80%9Cawk%E2%80%9D_Command\" >\u00a0\u00a0\u00a0\u00a0\u00a0 I. Using \u201cawk\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#_II_Using_%E2%80%9Cread%E2%80%9D_Command\" >\u00a0\u00a0\u00a0 II. Using \u201cread\u201d Command<\/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\/string\/manipulation\/string-array\/#_III_Using_%E2%80%9Ctr%E2%80%9D_Command\" >\u00a0 III. Using \u201ctr\u201d Command<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#Some_Tips_While_Manipulating_String_Arrays\" >Some Tips While Manipulating String Arrays<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#Conclusion\" >Conclusion<\/a><\/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\/string\/manipulation\/string-array\/#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-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#How_do_you_store_an_array_of_strings_in_Bash\" >How do you store an array of strings in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#How_do_you_append_a_string_to_an_array_in_Bash\" >How do you append a string to an array in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#How_do_you_convert_a_string_to_an_array_in_Bash\" >How do you convert a string to an 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\/string\/manipulation\/string-array\/#How_do_I_find_the_length_of_an_array_in_Bash\" >How do I find the length of an array in Bash?<\/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\/string\/manipulation\/string-array\/#How_to_create_array_in_Bash\" >How to create array in Bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"1_Declare_an_Array_of_Strings_in_Bash\"><\/span>1. Declare an Array of Strings in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In an index array, each array element is assigned across an index. To declare an index array of strings, follow the syntax:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">array_name=(&quot;Tommy&quot; &quot;Huge&quot; &quot;Mike&quot; &quot;Wade&quot;)<\/code><\/pre>\n<p>It will declare an array of strings with four array elements.<\/p>\n<p style=\"text-align: justify;\">On the other hand, the associative array uses key over number as indices. This type of array is useful for storing data in key-value pair format. Here, I will create an associative array and add two elements with keys:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ndeclare -A associative_array\nassociative_array[&quot;name&quot;]=&quot;Susmit&quot;\nassociative_array[&quot;country&quot;]=&quot;Bangladesh&quot;\n\necho ${associative_array[&quot;name&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 command <code class=\"\" data-line=\"\">declare -A associative_array<\/code> declares an associative array. Then <code class=\"\" data-line=\"\">associative_array[&quot;name&quot;]=&quot;Susmit&quot;<\/code> sets the value \u201cSusmit\u201d and \u201cBangladesh\u201d to the key \u201cname\u201d and \u201ccountry\u201d. Finally, the <a href=\"https:\/\/linuxsimply.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a> prints the value of associative_array having key \u201cname\u201d<\/p>\n<\/div><\/div><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-37536 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1-associative.png\" alt=\"Bash script has defined an associative array.\" width=\"695\" height=\"189\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1-associative.png 695w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1-associative-300x82.png 300w\" sizes=\"(max-width: 695px) 100vw, 695px\" \/><\/p>\n<p style=\"text-align: justify;\">As in the above image, an associative array has been declared.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"2_Loop_Through_String_Array_Indices\"><\/span>2. Loop Through String Array Indices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Looping through string array indices enables users to access the array elements and do the necessary operations. For accessing the array elements individually, <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\"><strong>for loop<\/strong><\/a> is the easiest approach. Here, I will loop through an array and print the elements on the terminal:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\narray_name=(&quot;Tommy&quot; &quot;Huge&quot; &quot;Mike&quot; &quot;Wade&quot;)\nfor i in ${!array_name[@]}; do\n  echo &quot;element $i is ${array_name[$i]}&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>Here, <code class=\"\" data-line=\"\">array_name=(&quot;Tommy&quot; &quot;Huge&quot; &quot;Mike&quot; &quot;Wade&quot;)<\/code> defines the array_name array that possesses four elements. After that, <code class=\"\" data-line=\"\">for i in ${!array_name[@]}; do<\/code> defines a for loop and iterates through each element of the array, and finally, the echo command prints the element on the terminal.<\/p>\n<\/div><\/div><img decoding=\"async\" class=\"aligncenter wp-image-37537 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2-array-element.png\" alt=\"The for loop has accessed all the elements of string array.\" width=\"639\" height=\"276\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2-array-element.png 639w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2-array-element-300x130.png 300w\" sizes=\"(max-width: 639px) 100vw, 639px\" \/><\/p>\n<p style=\"text-align: justify;\">The image shows that the loop has iterated through the array elements and printed them on the terminal.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"3_Add_and_Remove_Elements_of_the_Array_of_String\"><\/span>3. Add and Remove Elements of the Array of String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To append a new array element to an existing array, use the <code class=\"\" data-line=\"\">array_name+=(&quot;new_element&quot;)<\/code> syntax. And use the <code class=\"\" data-line=\"\">unset array_name[index]<\/code> command to remove any element from an array. Here is a Bash script to do so:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ncities=(&quot;Brisbane&quot; &quot;Sidney&quot; &quot;Hobart&quot;)\necho \u201cIntial array:\u201d\necho ${cities[@]}\ncities+=(&quot;Canberra&quot;)\necho \u201cAfter Adding an Element to the array:\u201d\necho ${cities[@]}\nunset cities[1]\necho \u201cAfter Removing an Element from the array:\u201d\necho ${cities[@]}<\/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>Here,<code class=\"\" data-line=\"\">cities+=(&quot;Canberra&quot;)<\/code> appends \u201cCanberra\u201d to the cities array. After that, <code class=\"\" data-line=\"\">unset cities[1]<\/code> removes the element at index 1, which is the second element of the array.<\/p>\n<\/div><\/div><img decoding=\"async\" class=\"aligncenter wp-image-37538 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3-add.png\" alt=\"Bash script has added an element to the array then deleted another element from the array.\" width=\"680\" height=\"405\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3-add.png 680w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3-add-300x179.png 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/p>\n<p style=\"text-align: justify;\">In the image, the bash script has appended a new element to the array and removed another element from the array.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Sort_Arrays_of_String\"><\/span>4. Sort Arrays of String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Sorting makes it easy for users to go through the strings inside an array and find the intended string. The sort command can print the contents of a file in a sorted manner. Here is a practical Bash script to do so:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\ncities=(&quot;Brisbane&quot; &quot;Sidney&quot; &quot;Hobart&quot;)\nsorted_cities=($(for i in &quot;${cities[@]}&quot;; do echo $i; done | sort))\necho \u201cAfter Sorting the Elements of the array:\u201d\necho ${sorted_cities[@]}<\/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>Here, <code class=\"\" data-line=\"\">sorted_cities=($(for i in &quot;${cities[@]}&quot;; do echo $i; done | sort))<\/code> defines a for loop that iterates over each element of the \u201ccities\u201d array and prints them on a new line. After that, the output of the for loop is piped to the <a href=\"https:\/\/linuxsimply.com\/sort-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>sort command<\/strong><\/a> that sorts the words of that line alphabetically and stores the sorted words in the \u201csorted_cities\u201d array.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-37539 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4-sort.png\" alt=\"Arrays elements are sorted.\" width=\"670\" height=\"253\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4-sort.png 670w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4-sort-300x113.png 300w\" sizes=\"(max-width: 670px) 100vw, 670px\" \/><\/p>\n<p style=\"text-align: justify;\">The image shows a sorted string array.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"5_Split_String_to_Array\"><\/span>5. Split String to Array<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">A string can be split by assigning the individual string words to a new array. For this, declare an empty array first, then iterate through each word of the main string using a <strong>for loop<\/strong> and append each of the words to the newly declared array. Here is the Bash script to do so:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\na=\u201dHello Welcome Here\u201d\nb=()\nfor i in $a; do\n  b+=($i) ;\ndone\nfor c in ${b[@]}; do\n  echo $c;\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 <code class=\"\" data-line=\"\">for i in $a; do<\/code> initiates a for loop that iterates over the words of string \u201ca\u201d individually by splitting them, considering \u201cspace\u201d as a delimiter, and storing them to the iteration variable \u201ci\u201d. Then, it appends each word to the array \u201cb\u201d. Finally, the second for loop iterates over each element of array \u201cb\u201d and prints them on the terminal.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-37540 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5-split.png\" alt=\"The bash script has converted the strings to array.\" width=\"599\" height=\"252\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5-split.png 599w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5-split-300x126.png 300w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"_I_Using_%E2%80%9Cawk%E2%80%9D_Command\"><\/span>\u00a0\u00a0\u00a0\u00a0\u00a0 I. Using \u201cawk\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/awk-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>awk command<\/strong><\/a> can split a string based on the specified delimited inside the split function. Here is a command to split a string based on space and print the 2nd element of the array:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">echo &quot;Hi Welcome Here&quot; | awk &#039;{split($0,a,&quot; &quot;); print a[2]}&#039;<\/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 <code class=\"\" data-line=\"\">\u201cHi Welcome Here\u201d<\/code> string is piped to the awk command. The awk command splits the input string into fields based on the delimiter specified as \u201c \u201d(space) utilizing the split function and stores them as the elements of a. Finally, <code class=\"\" data-line=\"\">print a[2]}&#039;<\/code> command prints the second element of the array \u2018a\u2019.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-37541 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6-awk.png\" alt=\"The awk command has converted the string to an array.\" width=\"825\" height=\"81\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6-awk.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6-awk-300x29.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6-awk-768x75.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"_II_Using_%E2%80%9Cread%E2%80%9D_Command\"><\/span>\u00a0\u00a0\u00a0 II. Using \u201cread\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/read-from-stdin\/\" target=\"_blank\" rel=\"noopener\"><strong>read command<\/strong><\/a> with the -a option reads string input from the user as an array. To read a string from the user, use the syntax: read -a [array_name]. The -a option will split the input string, considering space(\u201c \u201c) as an Internal Field Separator (IFS) character. Here is a bash script to read an array from the user and then print the element on the terminal:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nw=()\nread -a w\n\nfor b in ${w[@]}; do echo $b ; done<\/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><code class=\"\" data-line=\"\">read -a w<\/code> reads input from the user and stores it in the array \u2018w\u2019. Here, the -a option specifies to read the input as an array. Then, <code class=\"\" data-line=\"\">for b in ${w[@]}; do echo $b ; done<\/code> initiates a for loop that iterates over each element of w array and prints them individually after splitting them.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-37542 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7-read.png\" alt=\"The read command with -a option has converted the string to array.\" width=\"576\" height=\"273\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7-read.png 576w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7-read-300x142.png 300w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"_III_Using_%E2%80%9Ctr%E2%80%9D_Command\"><\/span>\u00a0 III. Using \u201ctr\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/tr-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>tr (translate) command<\/strong><\/a> can replace the delimiter with a newline character. Thus, it can convert a string to an array. Here is a complete bash script to convert string to array:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstring=&quot;Dhaka,Chattogram,Sylhet&quot;\narray=($(echo $string | tr &quot;,&quot; &quot;\\n&quot;))\nfor element in &quot;${array[@]}&quot;; do\n  echo &quot;$element&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 part, <code class=\"\" data-line=\"\">array=($(echo $string | tr &quot;,&quot; &quot;\\n&quot;))<\/code> pipes the &#8220;Dhaka, Chattogram, Sylhet&#8221; string to the tr command that replaces each \u201c,\u201d with a newline character \u2018\\n\u2019 and stores the elements to the array. Here, <code class=\"\" data-line=\"\">$( )<\/code> acts as a command substitution that captures the output of the tr command. Finally, the echo command inside a for loop prints each array element on the terminal.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-37543 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8-tr.png\" alt=\"The tr command has converted the string to array.\" width=\"550\" height=\"249\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8-tr.png 550w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8-tr-300x136.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Some_Tips_While_Manipulating_String_Arrays\"><\/span>Some Tips While Manipulating String Arrays<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>While manipulating string arrays, be careful of the below facts:<\/p>\n<p style=\"text-align: justify;\"><strong>Handling Spaces in Strings: <\/strong>In Bash, space is a delimiter. If space is present in the string, the bash will interpret it as separate elements.<\/p>\n<p style=\"text-align: justify;\"><strong>Dealing with Empty or Undefined Elements: <\/strong>If you try to access an undefined array element, it will return an empty string.<\/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;\">This article will guide users in effectively handling and manipulating the bash string array to achieve the modified value of a string. It is a necessary tool for text processing and parsing important information.<\/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_do_you_store_an_array_of_strings_in_Bash\"><\/span>How do you store an array of strings in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To store an array of strings in Bash, use the syntax <code class=\"\" data-line=\"\">arr=(\u201cGood\u201d \u201cMorning\u201d)<\/code>. It will create an array named arr, having two strings, \u201cGood\u201d and \u201cMorning\u201d as elements.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_you_append_a_string_to_an_array_in_Bash\"><\/span>How do you append a string to an array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To append a string to an array in Bash, use the syntax <code class=\"\" data-line=\"\">array_name+=(&#039;appended string&#039;)<\/code>. It will append the \u2018appended string\u2019 to the array_name array.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_you_convert_a_string_to_an_array_in_Bash\"><\/span>How do you convert a string to an array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To convert a string to an array in Bash, use the syntax <code class=\"\" data-line=\"\">read -a array &lt;&lt;&lt; \u201c$string\u201d<\/code>. It will read a line from the standard input and split it into an array.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_find_the_length_of_an_array_in_Bash\"><\/span>How do I find the length of an array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To find the length of an array in Bash, use the syntax <code class=\"\" data-line=\"\">echo \u201c${#array_name[@]}\u201d<\/code>. It will print the array length to the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_create_array_in_Bash\"><\/span>How to create array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To create an array in Bash, use the syntax <code class=\"\" data-line=\"\">array_name=(element1, element2, element3)<\/code>. It will create an array named array_name with three elements in it.<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"How do you store an array of strings in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To store an array of strings in Bash, use the syntax arr=(\u201cGood\u201d \u201cMorning\u201d). It will create an array named arr, having two strings, \u201cGood\u201d and \u201cMorning\u201d as elements.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do you append a string to an array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To append a string to an array in Bash, use the syntax array_name+=('appended string'). It will append the \u2018appended string\u2019 to the array_name array.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do you convert a string to an array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To convert a string to an array in Bash, use the syntax read -a array <<< \u201c$string\u201d. It will read a line from the standard input and split it into an array.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do I find the length of an array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To find the length of an array in Bash, use the syntax echo \u201c${#array_name[@]}\u201d. It will print the array length to the terminal.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to create array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To create an array in Bash, use the syntax array_name=(element1, element2, element3). It will create an array named array_name with three elements in it.\"\n    }\n  }]\n}\n<\/script><\/p>\n<p><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\/string\/manipulation\/string-to-int\/\" target=\"_blank\" rel=\"noopener\">How to Convert a Bash String to Int? [8 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/format-string\/\" target=\"_blank\" rel=\"noopener\">How to Format a String in Bash? [Methods, Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/convert-to-uppercase\/\" target=\"_blank\" rel=\"noopener\">How to Convert Bash String to Uppercase? [7 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/bash-to-lowercase\/\" target=\"_blank\" rel=\"noopener\">How to Convert Bash String to Lowercase? [7 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-replace\/\" target=\"_blank\" rel=\"noopener\">How to Replace String in Bash? [5 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/trim-string\/\" target=\"_blank\" rel=\"noopener\">How to Trim String in Bash? [6 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/truncate-string\/\" target=\"_blank\" rel=\"noopener\">How to Truncate String in Bash? [5 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/remove-character\/\" target=\"_blank\" rel=\"noopener\">How to Remove Character from String in Bash? [7+ Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/remove-first-character\/\" target=\"_blank\" rel=\"noopener\">How to Remove First Character From Bash String? [7 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/remove-last-character\/\" target=\"_blank\" rel=\"noopener\">How to Remove Last Character from Bash String [6 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-functions\/\" target=\"_blank\" rel=\"noopener\">How to Use String Functions in Bash? [Examples Included]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/generate-random-string\/\" target=\"_blank\" rel=\"noopener\">How to Generate a Random String in Bash? [8 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/here-string\/\" target=\"_blank\" rel=\"noopener\">How to Use \u201cHere String\u201d in Bash? [Basic to Advance]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/base64-encode-decode\/\" target=\"_blank\" rel=\"noopener\">Encode and Decode with \u201cbase64\u201d in Bash [6 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/bash-eval\/\" target=\"_blank\" rel=\"noopener\">How to Use \u201ceval\u201d Command in Bash? [8 Practical Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/get-first-character\/\" target=\"_blank\" rel=\"noopener\">How to Get the First Character from Bash String? [8 Methods]<\/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;String Manipulation 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\/string\/manipulation\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/\" target=\"_blank\" rel=\"noopener\">String Manipulation in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash String&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\/string\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/\" target=\"_blank\" rel=\"noopener\">Bash String<\/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><\/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;37510&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;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&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;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Manipulate Bash String Array [5 Ways]&quot;,&quot;width&quot;:&quot;0&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: 0px;\">\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            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>String stores user and system information. An array of strings stores a collection of strings in an array. For manipulating &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Manipulate Bash String Array [5 Ways]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#more-37510\" aria-label=\"Read more about How to Manipulate Bash String Array [5 Ways]\">Read more<\/a><\/p>\n","protected":false},"author":314894,"featured_media":37544,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[192],"class_list":["post-37510","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-string-manipulation","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>How to Manipulate Bash String Array [5 Ways] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"Bash string array stores vital information containg strings into the array. Users can manipulated this string array according to necessity.\" \/>\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\/string\/manipulation\/string-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Manipulate Bash String Array [5 Ways] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"Bash string array stores vital information containg strings into the array. Users can manipulated this string array according to necessity.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/dasgupta.susmit\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-22T08:48:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-31T06:17:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-array-feature-image.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=\"Susmit Das Gupta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Susmit Das Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 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\\\/string\\\/manipulation\\\/string-array\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/\"},\"author\":{\"name\":\"Susmit Das Gupta\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/38abbef9a1053d9faec8935dc39f10fa\"},\"headline\":\"How to Manipulate Bash String Array [5 Ways]\",\"datePublished\":\"2024-02-22T08:48:02+00:00\",\"dateModified\":\"2024-03-31T06:17:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/\"},\"wordCount\":1508,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/bash-string-array-feature-image.png\",\"keywords\":[\"string manipulation\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/\",\"name\":\"How to Manipulate Bash String Array [5 Ways] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/bash-string-array-feature-image.png\",\"datePublished\":\"2024-02-22T08:48:02+00:00\",\"dateModified\":\"2024-03-31T06:17:22+00:00\",\"description\":\"Bash string array stores vital information containg strings into the array. Users can manipulated this string array according to necessity.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/bash-string-array-feature-image.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/bash-string-array-feature-image.png\",\"width\":400,\"height\":400,\"caption\":\"Bash string array.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/string-array\\\/#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\":\"Bash String\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"String Manipulation in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/string\\\/manipulation\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Manipulate Bash String Array [5 Ways]\"}]},{\"@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\\\/38abbef9a1053d9faec8935dc39f10fa\",\"name\":\"Susmit Das Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Susmit-Das-Gupta-II-96x96.png\",\"caption\":\"Susmit Das Gupta\"},\"description\":\"Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/dasgupta.susmit\",\"https:\\\/\\\/www.instagram.com\\\/susmit_dasgupta\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/susmitdasgupta\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/susmit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Manipulate Bash String Array [5 Ways] - LinuxSimply","description":"Bash string array stores vital information containg strings into the array. Users can manipulated this string array according to necessity.","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\/string\/manipulation\/string-array\/","og_locale":"en_US","og_type":"article","og_title":"How to Manipulate Bash String Array [5 Ways] - LinuxSimply","og_description":"Bash string array stores vital information containg strings into the array. Users can manipulated this string array according to necessity.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/dasgupta.susmit","article_published_time":"2024-02-22T08:48:02+00:00","article_modified_time":"2024-03-31T06:17:22+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-array-feature-image.png","type":"image\/png"}],"author":"Susmit Das Gupta","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Susmit Das Gupta","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/"},"author":{"name":"Susmit Das Gupta","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/38abbef9a1053d9faec8935dc39f10fa"},"headline":"How to Manipulate Bash String Array [5 Ways]","datePublished":"2024-02-22T08:48:02+00:00","dateModified":"2024-03-31T06:17:22+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/"},"wordCount":1508,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-array-feature-image.png","keywords":["string manipulation"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/","name":"How to Manipulate Bash String Array [5 Ways] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-array-feature-image.png","datePublished":"2024-02-22T08:48:02+00:00","dateModified":"2024-03-31T06:17:22+00:00","description":"Bash string array stores vital information containg strings into the array. Users can manipulated this string array according to necessity.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-array-feature-image.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/bash-string-array-feature-image.png","width":400,"height":400,"caption":"Bash string array."},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/string-array\/#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":"Bash String","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/"},{"@type":"ListItem","position":4,"name":"String Manipulation in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/string\/manipulation\/"},{"@type":"ListItem","position":5,"name":"How to Manipulate Bash String Array [5 Ways]"}]},{"@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\/38abbef9a1053d9faec8935dc39f10fa","name":"Susmit Das Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Susmit-Das-Gupta-II-96x96.png","caption":"Susmit Das Gupta"},"description":"Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/dasgupta.susmit","https:\/\/www.instagram.com\/susmit_dasgupta\/","https:\/\/www.linkedin.com\/in\/susmitdasgupta\/"],"url":"https:\/\/linuxsimply.com\/author\/susmit\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/37510","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\/314894"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=37510"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/37510\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/37544"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=37510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=37510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=37510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}