{"id":26793,"date":"2023-11-14T05:24:46","date_gmt":"2023-11-14T05:24:46","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=26793"},"modified":"2024-04-23T04:07:01","modified_gmt":"2024-04-23T04:07:01","slug":"bash-print-array","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/","title":{"rendered":"How to Print an Array in Bash? [5 Cases]"},"content":{"rendered":"<p>You can use the following 5 methods to <strong>print an array in Bash<\/strong>:<\/p>\n<ol>\n<li>To<strong> print the entire array:<br \/>\n<\/strong><code class=\"\" data-line=\"\">${your_array[@ or *]}<\/code><\/li>\n<li>To<strong> print the array index, value, and type:<br \/>\n<\/strong><code class=\"\" data-line=\"\">declare -p &lt;your_array&gt;<\/code><\/li>\n<li>To<strong> print the array iteratively:<br \/>\n<\/strong><code class=\"\" data-line=\"\">for item in ${your_array[@]} do echo $item done<\/code><\/li>\n<li>To<strong> print the array in new lines:<br \/>\n<\/strong><code class=\"\" data-line=\"\">printf &quot;%s\\n&quot; &quot;${array_name[@]}&quot;<\/code><\/li>\n<li>To<strong> print specific elements from an array<\/strong>:<br \/>\n<code class=\"\" data-line=\"\">echo &quot;${array[index_number]}&quot;<\/code><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">The rest of the article represents an in-depth analysis of how to adopt these <strong>5 methods to print arrays in Bash<\/strong>.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/bash-print-array-practice-files.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 Print Arrays 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\/array\/print-array\/#1_Print_an_Array_in_Bash_Using_Length_Expression\" >1. Print an Array in Bash Using Length Expression<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#Printing_an_Associative_Array_Using_Length_Expression\" >Printing an Associative Array Using Length Expression<\/a><\/li><\/ul><\/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\/array\/print-array\/#2_Print_an_Array_in_Bash_Using_the_%E2%80%9Cdeclare_-p%E2%80%9D_Command\" >2. Print an Array in Bash Using the \u201cdeclare -p\u201d Command<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#Print_an_Associative_Array_Using_the_%E2%80%9Cdeclare_-p%E2%80%9D_Command\" >Print an Associative Array Using the \u201cdeclare -p\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-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#3_Print_an_Array_in_Bash_Using_Loops\" >3. Print an Array in Bash Using Loops<\/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\/array\/print-array\/#Printing_an_Associative_Array_Using_Loop\" >Printing an Associative Array Using Loop<\/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\/array\/print-array\/#Print_Array_in_Reverse_Order\" >Print Array in Reverse Order<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#4_Print_an_Array_in_Bash_Using_the_%E2%80%9Cprintf%E2%80%9D_Command\" >4. Print an Array in Bash Using the \u201cprintf\u201d Command<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#Print_an_Associative_Array_Using_the_%E2%80%9Cprintf%E2%80%9D_Command\" >Print an Associative Array Using the \u201cprintf\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-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#5_Print_Specific_Elements_of_an_Array_in_Bash\" >5. Print Specific Elements of an Array in Bash<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#Using_Array_Slicing_Methods\" >Using Array Slicing Methods<\/a><\/li><\/ul><\/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\/array\/print-array\/#Print_an_Array_to_a_File_in_Bash\" >Print an Array to a File in Bash<\/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\/array\/print-array\/#Print_Array_Elements_with_Newlines_in_Bash\" >Print Array Elements with Newlines in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-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-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#Can_I_print_specific_elements_of_an_array_in_Bash\" >Can I print specific elements 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-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#How_to_print_a_Bash_array\" >How to print a Bash array?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#What_is_the_difference_between_the_%E2%80%9Carray%E2%80%9D_and_%E2%80%9Carray%E2%80%9D_in_Bash\" >What is the difference between the \u201c${array[@]}\u201d and \u201c${array[*]}\u201d in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#What_is_a_Bash_Array\" >What is a Bash Array?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#How_to_echo_array_in_Bash_terminal\" >How to echo array in Bash terminal?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#How_to_print_the_length_of_an_array_in_Bash\" >How to print 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-22\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#Can_I_print_an_array_with_new_lines_in_Bash\" >Can I print an array with new lines in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#How_do_I_print_the_key-value_pairs_of_an_associative_array_in_a_single_line\" >How do I print the key-value pairs of an associative array in a single line?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#Is_it_possible_to_display_the_indices_of_an_indexed_array_in_Bash\" >Is it possible to display the indices of an indexed array in Bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"1_Print_an_Array_in_Bash_Using_Length_Expression\"><\/span>1. Print an Array in Bash Using Length Expression<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Length expression <code class=\"\" data-line=\"\">${array[@]}<\/code> or <code class=\"\" data-line=\"\">${array[*]}<\/code> prints all the items of an indexed array located at the specified indices simply with the <strong><a href=\"https:\/\/linuxsimply.com\/echo-command-in-linux\/#Example_3_Using_the_echo_Command_to_View_Variables_in_the_Command_Line\" target=\"_blank\" rel=\"noopener\">echo<\/a><\/strong> command.<\/p>\n<p style=\"text-align: justify;\">Here\u2019s how to print an indexed array in Bash using the length expressions <code class=\"\" data-line=\"\">${array[@]}<\/code> and <code class=\"\" data-line=\"\">${array[*]}<\/code> :<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Array creation\nblack=(jon tahsan jahan tony meraz)\n\n#printing array length to the terminal using both expressions\necho &quot;the array elements printed using \\${black[@]}: ${black[@]}&quot;\necho               # for adding extra spaces between the outputs\necho &quot;the array elements printed using \\${black[*]}: ${black[*]}&quot;\n<\/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 style=\"text-align: justify;\">In the above script, the length expression <code class=\"\" data-line=\"\">${black[@]}<\/code> and <code class=\"\" data-line=\"\">${black[*]}<\/code> access the array <strong>black<\/strong> with 5 elements. Finally, the echo command displays all the array items in a single line to the prompt.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\">The output of the above code will look like this:<img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-26799 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1-idx-array-print-expressions.png\" alt=\"bash print indexed array using length expressions\" width=\"825\" height=\"210\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1-idx-array-print-expressions.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1-idx-array-print-expressions-300x76.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/1-idx-array-print-expressions-768x195.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/> The above snapshot states that the commands <code class=\"\" data-line=\"\">echo ${black[@]}<\/code> and <code class=\"\" data-line=\"\">echo ${black[*]}<\/code> both seamlessly print the 5 individual elements of the array <strong>black <\/strong>in a single line separated by space.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Printing_an_Associative_Array_Using_Length_Expression\"><\/span>Printing an Associative Array Using Length Expression<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To print the keys and values of an associative array in Bash, you can use the primary expression <code class=\"\" data-line=\"\">${my_associative[@]}<\/code>. Here\u2019s an example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#declare an associative array and assign values\ndeclare -A my_associative\nmy_associative[&quot;eat&quot;]=&quot;rice&quot;\nmy_associative[&quot;take&quot;]=&quot;tea&quot;\nmy_associative[&quot;drink&quot;]=&quot;juice&quot;\n\n#printing the array keys\necho &quot;keys: ${!my_associative[@]}&quot;\n\n#printing the array values\necho &quot;values: ${my_associative[@]}&quot;\n<\/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 style=\"text-align: justify;\">Here, the expression <code class=\"\" data-line=\"\">echo keys: ${!my_associative[@]}<\/code> prints the keys <strong>(take drink eat)<\/strong> of the associative array called <strong>my_associative<\/strong>. Next, the <code class=\"\" data-line=\"\">echo values: ${my_associative[@]}<\/code> expression displays the values <strong>(tea juice rice)<\/strong> of the array in the following line.<\/p>\n<p style=\"text-align: justify;\"><\/div><\/div><img decoding=\"async\" class=\"aligncenter wp-image-26800 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2-assoc-print-expression.png\" alt=\"bash print associative array using length expression\" width=\"825\" height=\"163\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2-assoc-print-expression.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2-assoc-print-expression-300x59.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/2-assoc-print-expression-768x152.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The above Bash script successfully prints the associative array \u201cmy_associative\u201d (<strong>both the keys and values<\/strong>) using length expressions\u00a0 <code class=\"\" data-line=\"\">${!my_associative[@]}<\/code> and <code class=\"\" data-line=\"\">${my_associative[@]}<\/code> respectively.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"2_Print_an_Array_in_Bash_Using_the_%E2%80%9Cdeclare_-p%E2%80%9D_Command\"><\/span>2. Print an Array in Bash Using the \u201cdeclare -p\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <a href=\"https:\/\/linuxsimply.com\/declare-command-in-linux\/#Example_7_PrintOut_the_Options_and_Attributes_of_the_Variables\" target=\"_blank\" rel=\"noopener\"><strong>declare -p<\/strong><\/a> command in Bash <strong>displays the current state of a variable<\/strong> and, regarding arrays, prints<strong> the elements of the array<\/strong> including its name, variable type, and index.<\/p>\n<p>Below is a script to print an indexed array in Bash with index and details using the <code class=\"\" data-line=\"\">declare -p<\/code> command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#declare an array and populate its values\ndeclare -a cadbury=(dairy milk silk)\n\n#use declare -p to print the array \ndeclare -p cadbury \n<\/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 style=\"text-align: justify;\">In this code snippet, after the declaration of the indexed array <strong>cadbury<\/strong> using the syntax <code class=\"\" data-line=\"\">declare -a cadbury=(dairy milk silk)<\/code>, the <code class=\"\" data-line=\"\">declare -p<\/code> command prints all the elements of that array along with displaying its name, type, and indices.<\/p>\n<\/div><\/div>\n<p>The output will look like this:<img decoding=\"async\" class=\"aligncenter wp-image-26801 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3-idx-arr-print-using-declareP.png\" alt=\"printing indexed bash array using declare -p command\" width=\"825\" height=\"118\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3-idx-arr-print-using-declareP.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3-idx-arr-print-using-declareP-300x43.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/3-idx-arr-print-using-declareP-768x110.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/> The aforementioned image states that the <code class=\"\" data-line=\"\">declare<\/code> command with the <code class=\"\" data-line=\"\">-p<\/code> option prints the items of the array named <strong>cadbury <\/strong>with its respective indices.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Print_an_Associative_Array_Using_the_%E2%80%9Cdeclare_-p%E2%80%9D_Command\"><\/span>Print an Associative Array Using the \u201cdeclare -p\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The below script shows an example of how to use <code class=\"\" data-line=\"\">declare -p<\/code> to print an associative array:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#declare an associative array\ndeclare -A assoc_array\n\n#value insertion to the array\nassoc_array[&quot;fruit&quot;]=&quot;apple&quot;\nassoc_array[&quot;vegetable&quot;]=&quot;carrot&quot;\nassoc_array[&quot;drink&quot;]=&quot;water&quot;\n\n#use declare -p to print the elements and details\ndeclare -p assoc_array\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26802 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4-assoc-print-declareP.png\" alt=\"printing bash associative array using declare -p\" width=\"825\" height=\"109\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4-assoc-print-declareP.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4-assoc-print-declareP-300x40.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/4-assoc-print-declareP-768x101.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>In the snap above, you see that the <code class=\"\" data-line=\"\">declare -p<\/code> command prints the <strong>array items, indices, name, and type<\/strong> in a single line of the associative array named\u00a0 <strong>assoc_array<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"3_Print_an_Array_in_Bash_Using_Loops\"><\/span>3. Print an Array in Bash Using Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Using <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\"><strong>loop<\/strong><\/a> is an effective technique to perform tasks requiring repetition such as printing the elements of an indexed or associative array in Bash. For instance, the <code class=\"\" data-line=\"\">for ((i=0; i&lt;5; i++))<\/code> loop will do the specified task 5 times in an iterative manner.<\/p>\n<p>To print the elements of an indexed array in Bash using a <strong>for<\/strong> <strong>loop<\/strong>, see the below script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#Declaring an indexed array and assigning it with values \ncoffee=(latte americano cortado espresso)\n\n#looping through the elements to print \nfor elements in ${coffee[@]}\n    do\n        echo $elements\ndone \n<\/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 style=\"text-align: justify;\">First, the complete looping expression <code class=\"\" data-line=\"\">for elements in ${coffee[@]}<\/code> iterates through the <strong>coffee<\/strong> array until it has covered the entire array length <strong>(which is 4)<\/strong> and prints the items into the terminal iteratively on new lines using <code class=\"\" data-line=\"\">echo $elements<\/code>.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26803 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5-idx-arr-print-for-loop.png\" alt=\"printing bash indexed array using for loop\" width=\"825\" height=\"247\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5-idx-arr-print-for-loop.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5-idx-arr-print-for-loop-300x90.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/5-idx-arr-print-for-loop-768x230.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>From the output, you may notice that the for loop prints the 4 elements of the <strong>coffee<\/strong> array in prints them on 4 new lines.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Printing_an_Associative_Array_Using_Loop\"><\/span>Printing an Associative Array Using Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The for loop prints a Bash associative array <strong>(both the keys and values)<\/strong> successfully. Here\u2019s an example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#declare an associative array\ndeclare -A my_assoc_array\n\n#populate the array with values\nmy_assoc_array[&quot;fruit&quot;]=&quot;apple&quot;\nmy_assoc_array[&quot;vegetable&quot;]=&quot;carrot&quot;\nmy_assoc_array[&quot;drink&quot;]=&quot;water&quot;\n\n#for loop to print the array keys and values\nfor key in &quot;${!my_assoc_array[@]}&quot;; do\n  value=&quot;${my_assoc_array[$key]}&quot;\n  echo &quot;Key: $key, Value: $value&quot;\ndone\n<\/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, the for loop first iterates through the keys of the <strong>my_assoc_array <\/strong>by <code class=\"\" data-line=\"\">for key in &quot;${!my_assoc_array[@]}&quot;;<\/code> to store the corresponding element into a variable called <strong>value<\/strong> and finally prints the <strong>key-value<\/strong> pairs in the same line for each element using <code class=\"\" data-line=\"\">echo &quot;Key: $key, Value: $value<\/code>. The for loop repeats the same task until it has reached the end of the array.<\/p>\n<\/div><\/div>\n<p style=\"text-align: left;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26804 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6-assoc-print-loop.png\" alt=\"bash associative array print using for loop\" width=\"825\" height=\"174\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6-assoc-print-loop.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6-assoc-print-loop-300x63.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/6-assoc-print-loop-768x162.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>Script output to print an associative array in Bash using for loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Print_Array_in_Reverse_Order\"><\/span>Print Array in Reverse Order<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Printing an array in the reverse order means displaying the elements in the opposite order from their original sequence. For example, the output after printing the array <strong>nums_array=(1 2 3 4) <\/strong>in the reverse order would be <strong>4 3 2 1<\/strong>. One easy way to print a Bash array in reverse is to use the <strong>for<\/strong> loop.<\/p>\n<p style=\"text-align: justify;\">To print an indexed array in the reverse order, you can use the loop <code class=\"\" data-line=\"\">for ((i = length - 1; i &gt;= 0; i--))<\/code> in a script like the following:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#creating the array with 4 elements \nnums_array=(1 2 3 4)\n\n#print the original array\necho \u201coriginal array: ${nums_array[*]}\u201d\necho #for extra space\n\n# Get the length of the array\nlength=${#nums_array[@]}\n\necho \u201creversed array:\u201d\n# Iterate through the array in reverse order and print \nfor ((i = length - 1; i &gt;= 0; i--)); do\n  echo &quot;${nums_array[i]}&quot;\ndone\n<\/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 style=\"text-align: justify;\">The for loop condition <code class=\"\" data-line=\"\">for ((i = length - 1; i &gt;= 0; i--));<\/code> states that the iteration of the <strong>nums_array <\/strong>starts fromthe last element and continues till the first element is located at index 0. In this repetitive manner, the for loop accesses the array in reverse and displays them to the prompt employing <code class=\"\" data-line=\"\">echo &quot;${nums_array[i]}&quot;<\/code>.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26805 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/7-print-array-reverse.png\" alt=\"print bash indexed array in reverse\" width=\"825\" height=\"365\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/7-print-array-reverse.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/7-print-array-reverse-300x133.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/7-print-array-reverse-768x340.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The above script prints the <strong>nums_array<\/strong> in its original order and then, in the reverse order <strong>(4 3 2 1)<\/strong> iteratively using the for loop.<\/p>\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;\">\n<p style=\"text-align: justify;\"><strong>NOTE<\/strong>: Associative arrays do not have any innate particular order whereas indexed arrays keep track of order using <strong>indices<\/strong>. Therefore, printing an <strong>associative array<\/strong> <strong>in reverse<\/strong> is not meaningful.<\/p>\n<\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"4_Print_an_Array_in_Bash_Using_the_%E2%80%9Cprintf%E2%80%9D_Command\"><\/span>4. Print an Array in Bash Using the \u201cprintf\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Apart from the standard echo command, the <code class=\"\" data-line=\"\">printf<\/code> command prints the elements of an array in Bash.<\/p>\n<p>To print an indexed array in Bash using the <code class=\"\" data-line=\"\">printf<\/code> command, see the example below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#create an array with 4 elements\nfoods=(&quot;apple&quot; &quot;milk&quot; &quot;flakes&quot; &quot;date&quot;)\n\n# Using printf to print array elements on a new line\nprintf &quot;%s\\n&quot; &quot;${foods[@]}&quot;\n<\/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 style=\"text-align: justify;\">In the above code, the <code class=\"\" data-line=\"\">printf<\/code> command prints the items of the array named <strong>foods<\/strong> using the expression <code class=\"\" data-line=\"\">${foods[@]}<\/code>. The <code class=\"\" data-line=\"\">\\n<\/code> is a newline character that tells <code class=\"\" data-line=\"\">printf<\/code> to display elements with newlines. The <code class=\"\" data-line=\"\">%s<\/code> accepts the elements as an argument.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26839 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/8-idx-arr-printf1.png\" alt=\"print bash indexed array with printf\" width=\"825\" height=\"231\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/8-idx-arr-printf1.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/8-idx-arr-printf1-300x84.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/8-idx-arr-printf1-768x215.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>In the above-captured snap, you see that the <code class=\"\" data-line=\"\">printf<\/code>\u00a0 command prints the array elements each on a new line.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Print_an_Associative_Array_Using_the_%E2%80%9Cprintf%E2%80%9D_Command\"><\/span>Print an Associative Array Using the \u201cprintf\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To print an associative array in Bash, you can use the <code class=\"\" data-line=\"\">printf<\/code> command. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#declare an associative array\ndeclare -A legends\n\n#assign values using keys\nlegends[&quot;virat&quot;]=&quot;kohli&quot;\nlegends[&quot;joe&quot;]=&quot;root&quot;\nlegends[&quot;kane&quot;]=&quot;williamson&quot;\n\n#storing keys and values into 2 arrays named keys and values respectively\nkeys=(&quot;${!legends[@]}&quot;)\nvalues=(&quot;${legends[@]}&quot;)\n\n#printing the keys and values using the printf command\nprintf &quot;Keys: %s\\n&quot; &quot;${keys[*]}&quot;\nprintf &quot;Values: %s\\n&quot; &quot;${values[*]}&quot;\n<\/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 style=\"text-align: justify;\">In the code above, the <code class=\"\" data-line=\"\">printf<\/code> command takes the keys and values of the legends array and stores them into 2 arrays named <strong>keys<\/strong> and <strong>values<\/strong> using the expressions <code class=\"\" data-line=\"\">(&quot;${!legends[@]}&quot;)<\/code> and\u00a0 <code class=\"\" data-line=\"\">(&quot;${legends[@]}&quot;)<\/code>. Finally, the<code class=\"\" data-line=\"\"> printf<\/code> command prints the keys and values in 2 separate lines because the expressions have an <strong>asterisk (*)<\/strong> instead of <strong>@<\/strong> which treats the entire array as a single argument.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26807 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/9-assoc-printf.png\" alt=\"bash print associative array using printf command\" width=\"825\" height=\"146\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/9-assoc-printf.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/9-assoc-printf-300x53.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/9-assoc-printf-768x136.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The above snap exemplifies that the <code class=\"\" data-line=\"\">printf<\/code> command prints the keys and associated items of the array <strong>legends<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"5_Print_Specific_Elements_of_an_Array_in_Bash\"><\/span>5. Print Specific Elements of an Array in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To print specific elements out of an entire array in Bash, use array indexing and array slicing methods. The array indexing method uses the syntax <strong>echo ${array_name[idx]}<\/strong> to print the corresponding item by taking the index number as input in place of <strong>idx<\/strong>.<\/p>\n<p>See below to print 4 elements out of 7 elements of an array using <code class=\"\" data-line=\"\">${array_name[idx]}<\/code>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#create an indexed array\nnum=(1 2 3 4 5 6 7)\n\n#print all items\necho &quot;all items: ${num[@]}&quot;\n\necho  # for an extra spacing \n\n#print specific items \necho &quot;using \\${num[idx]}&quot;\necho &quot;${num[2]}&quot;\necho &quot;${num[3]}&quot;\necho &quot;${num[4]}&quot;\necho &quot;${num[5]}&quot;\n<\/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 style=\"text-align: justify;\">Here, the command <code class=\"\" data-line=\"\">${num[idx]}<\/code> takes as input the indices <strong>(2 3 4 5)<\/strong> 4 times to print the 3rd to 6th elements in 4 new lines.<\/p>\n<\/div><\/div>\n<p>The code output will look like this:<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26842 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10-idx-array-print-spec-elems0.png\" alt=\"bash printing specific 4 items from 7 items using index\" width=\"478\" height=\"239\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10-idx-array-print-spec-elems0.png 478w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10-idx-array-print-spec-elems0-300x150.png 300w\" sizes=\"(max-width: 478px) 100vw, 478px\" \/> Script output to print 4 elements of the array <strong>num <\/strong>out of 7 elements using the above script.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Using_Array_Slicing_Methods\"><\/span>Using Array Slicing Methods<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In the array slicing method, there are two command-centric syntaxes: <strong>echo ${array_name[@ or *]: Start}<\/strong> and\u00a0 <strong>${array_name[@ or *]:Start:Count}<\/strong>.<\/p>\n<p style=\"text-align: justify;\">The 1st command <code class=\"\" data-line=\"\">${array_name[@ or *]:Start}<\/code> starts printing elements from the <strong>Start<\/strong> index up to the end. For instance, if the value of Start is 2, then the command prints all the values till the end from the 3rd element (index 2) till the end.<\/p>\n<p>To print all the array items from index 3, you can use the command\u00a0 <code class=\"\" data-line=\"\">${array_name[@ or *]:3}<\/code>. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#create an indexed array\nfood=(&quot;apple&quot; &quot;banana&quot; &quot;cherry&quot; &quot;date&quot; &quot;grape&quot;)\n\n#print all elements from index 3\necho ${food[@]:3}\n<\/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 style=\"text-align: justify;\">This code snippet uses the command <code class=\"\" data-line=\"\">echo ${food[@]:3}<\/code> to start printing the elements of the <strong>food <\/strong>array from<strong> index 3 (<\/strong>date<strong>) <\/strong>to the end of the array <strong>(<\/strong>grape<strong>)<\/strong>.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26808 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10-spec-elems.png\" alt=\"print 2 specific items of a bash array\" width=\"662\" height=\"132\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10-spec-elems.png 662w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/10-spec-elems-300x60.png 300w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/>Script output to print the 4th and 5th element <strong>(date grape)<\/strong> of the food array out of 5 elements using the command <code class=\"\" data-line=\"\">echo ${food[@]:3}<\/code>.<\/p>\n<p style=\"text-align: justify;\">In addition, the slicing expression <strong>${array_name[@ or *]:Start:Count}<\/strong> slices the elements of an indexed array beginning from the index <strong>Start <\/strong>(0,1,2\u2026) and extracts the number of elements specified by <strong>Count<\/strong>. For example, the command <code class=\"\" data-line=\"\">echo ${array_name[@ or *]:1:4}<\/code> prints 4 elements starting from index 1.<\/p>\n<p>To print 3 elements from an array of 7 elements starting from index 3, you can use echo <code class=\"\" data-line=\"\">${array_name[@ or *]:3:3}<\/code>. Here\u2019s an example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#create an indexed array\nfruits=(&quot;apple&quot; &quot;banana&quot; &quot;cherry&quot; &quot;date&quot; &quot;grape&quot; &quot;peach&quot; &quot;dragon&quot;)\n\n#print entire array\necho ${fruits[*]}\n\n#print 3 elements starting from index 3\necho ${fruits[@]:3:3}\n<\/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 style=\"text-align: justify;\">Here, the expression\u00a0 <code class=\"\" data-line=\"\">${fruits[@]:3:3}<\/code> begins from the 3rd index and accesses 3 elements inclusive. The <code class=\"\" data-line=\"\">echo<\/code> command then prints the items to the prompt.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26809 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/11-spec-elems2.png\" alt=\"printing 3 elements from 7 elements of a bash array\" width=\"825\" height=\"175\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/11-spec-elems2.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/11-spec-elems2-300x64.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/11-spec-elems2-768x163.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/>The above snapshot states that the <code class=\"\" data-line=\"\">echo ${fruits[@]:3:3}<\/code> command prints 3 elements <strong>(date grape peach) <\/strong>out of 7 elements starting from the 4th element.<\/p>\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;\">\n<p style=\"text-align: justify;\"><strong>NOTE<\/strong>: To print an associative array with specific elements, the key-based accessing method using the command <code class=\"\" data-line=\"\">echo &quot;${assoc_array[&quot;key&quot;]}&quot;<\/code> is recommended. <\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Print_an_Array_to_a_File_in_Bash\"><\/span>Print an Array to a File in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To print the elements of an array to a file, use the output redirection operator <strong>&gt; or &gt;&gt;<\/strong>. The <strong>&gt;<\/strong> operator creates the output file if it doesn\u2019t exist, redirects contents, and overwrites any extant content of that file during subsequent redirections. Contrarily, the <strong>&gt;&gt;<\/strong> operator appends the contents with the existing file items.<\/p>\n<p style=\"text-align: justify;\">For example, to print an array to a file named <strong>output.txt<\/strong> using both the expressions <code class=\"\" data-line=\"\">&quot;${your_array[@]}&quot; &gt; output.txt<\/code> and <code class=\"\" data-line=\"\">&quot;${your_array[@]}&quot; &gt;&gt; output.txt<\/code>, see the below code:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#create an array with elements\nlang_chain=(&quot;java&quot; &quot;mojo&quot; &quot;golang&quot; &quot;python&quot;)\n\n# Redirect the array contents to a file (overwriting the file if it exists)\nprintf &quot;%s\\n&quot; &quot;${lang_chain[@]}&quot; &gt; output.txt\necho # extra spacing \n# same work (appending this time) with echo command\necho &quot;${lang_chain[@]}&quot; &gt;&gt; output.txt\n\n#displaying contents of the file \ncat output.txt\n<\/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 style=\"text-align: justify;\">The expression <code class=\"\" data-line=\"\">printf &quot;%s\\n&quot; &quot;${lang_chain[@]}&quot; &gt; output.txt<\/code> redirects the output of <code class=\"\" data-line=\"\">printf &quot;%s\\n&quot; &quot;${lang_chain[@]}&quot;<\/code> (the array items) to the <strong>output.txt<\/strong> file. The command <code class=\"\" data-line=\"\">echo &quot;${lang_chain[@]}&quot; &gt;&gt; output.txt<\/code> does the same but this time it appends the contents with no overwriting of the existing content (output of the earlier command). The <a href=\"https:\/\/linuxsimply.com\/cat-command-in-linux\/#Example_1_Display_the_Contents_of_a_Single_File\"><strong>cat command<\/strong><\/a>, syntax: <code class=\"\" data-line=\"\">cat output.txt<\/code>, displays the contents for further verification.<\/p>\n<\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26810 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/12-print-to-file.png\" alt=\"print bash array to a file\" width=\"682\" height=\"254\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/12-print-to-file.png 682w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/12-print-to-file-300x112.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/>In the above image, you spot that the &gt; and &gt;&gt; redirection operators print the array items into the file named <strong>output.txt<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Print_Array_Elements_with_Newlines_in_Bash\"><\/span>Print Array Elements with Newlines in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To break the array and print the items with newlines, you can use the command <code class=\"\" data-line=\"\">printf &quot;%s\\n&quot; &quot;${array_name[@]}&quot;<\/code>. Look at the following example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n#create an array with elements \nhello=(&quot;world&quot; &quot;universe&quot; &quot;multiverse&quot;)\n\n# Print the array items on separate lines without a loop\nprintf &quot;%s\\n&quot; &quot;${hello[@]}&quot;\n<\/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 style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">printf<\/code> command with specifier <code class=\"\" data-line=\"\">&quot;%s\\n&quot;<\/code> prints the array <strong>hello<\/strong>, where the newline character <code class=\"\" data-line=\"\">\\n<\/code> denotes to print each element with newlines and format specifier <code class=\"\" data-line=\"\">%s<\/code>,\u00a0tells <code class=\"\" data-line=\"\">printf<\/code> to expect array items as arguments.<\/p>\n<p style=\"text-align: left;\"><\/div><\/div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-26811 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/13-print-with-newline.png\" alt=\"bash print array with new lines\" width=\"662\" height=\"222\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/13-print-with-newline.png 662w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/13-print-with-newline-300x101.png 300w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/>Script output to print a Bash array with newlines using the <code class=\"\" data-line=\"\">printf<\/code> command<\/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 this extensive guide, I have mentioned 5 methods to print an array in the Bash scripting language covering both the indexed and associative arrays. Choose the method that best suits your needs. The loop is useful when you want to perform an action on each element, while directly referencing elements is convenient for printing specific elements or the entire array. Happy scripting!<\/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=\"Can_I_print_specific_elements_of_an_array_in_Bash\"><\/span>Can I print specific elements of an array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes,<\/strong> you can use the array indices with the echo command like this: <strong>echo ${array[index_number]}<\/strong> to print the specific element. For instance, to print the 2nd element, use <code class=\"\" data-line=\"\">echo ${array[1]}<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_print_a_Bash_array\"><\/span>How to print a Bash array?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To print a Bash array:<\/p>\n<ol>\n<li>Use the syntax: <code class=\"\" data-line=\"\">echo ${array_name[@]}<\/code><br \/>\nOR,<\/li>\n<li>Use a for loop to print elements iteratively:<\/li>\n<\/ol>\n<pre><code class=\"language-bash\" data-line=\"\">for elements in &quot;${my_array[@]}&quot;; do\n  echo &quot;$elements&quot;\ndone\n<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_the_%E2%80%9Carray%E2%80%9D_and_%E2%80%9Carray%E2%80%9D_in_Bash\"><\/span>What is the difference between the \u201c${array[@]}\u201d and \u201c${array[*]}\u201d in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Both the expressions with the <code class=\"\" data-line=\"\">echo<\/code> command print all the array items. However, <code class=\"\" data-line=\"\">\u201c${array[@]}\u201d<\/code> treats each item as a separate entity preserving spaces while <code class=\"\" data-line=\"\">\u201c${array[*]}\u201d<\/code> combining all the elements and treating them as a single string.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_a_Bash_Array\"><\/span>What is a Bash Array?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A Bash array is a data structure used to store information in an indexed way. The <strong>indexed array<\/strong> in Bash has numbered indices while the <strong>associative arrays<\/strong> have string indices called keys. Unlike other programming languages, Bash arrays can store elements of various data types.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_echo_array_in_Bash_terminal\"><\/span>How to echo array in Bash terminal?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To echo an array in a Bash terminal, you can use the <code class=\"\" data-line=\"\">echo &quot;${array_name[@]}&quot;<\/code> syntax. This will output all elements of the array separated by spaces.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_print_the_length_of_an_array_in_Bash\"><\/span>How to print the length of an array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To print the length of an array in Bash, use the <code class=\"\" data-line=\"\">echo ${#array[@ or *]}<\/code> command. It will print the number of elements presented in the array. For example, to print the length of the array <strong>num=(1 2 3)<\/strong>:<\/p>\n<p><code class=\"\" data-line=\"\">echo ${#num[@]}<\/code><\/p>\n<p>The output will be 3.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_I_print_an_array_with_new_lines_in_Bash\"><\/span>Can I print an array with new lines in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>Yes<\/strong>, you can. To print bash arrays with new lines:<\/p>\n<ol>\n<li>Use Bash<strong> for loop<\/strong> to iterate over the elements to print them on separate lines like the following:\n<pre><code class=\"language-bash\" data-line=\"\">for item in &quot;${my_array[@]}&quot;; do\n  echo &quot;$item&quot;\ndone\n<\/code><\/pre>\n<\/li>\n<li>Use the<code class=\"\" data-line=\"\"> printf &#039;%s\\n&#039; &quot;${my_array[@]}&quot;<\/code> expression to print array items in newlines.<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_print_the_key-value_pairs_of_an_associative_array_in_a_single_line\"><\/span>How do I print the key-value pairs of an associative array in a single line?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To print the key-value pairs of an associative array, use the command <code class=\"\" data-line=\"\">declare -p<\/code> followed by the <strong>associative array name<\/strong> that you created earlier to fetch the items as well as their corresponding keys. The syntax is: <code class=\"\" data-line=\"\">declare -p associative_array_name<\/code>. However, the keys are printed in random order.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Is_it_possible_to_display_the_indices_of_an_indexed_array_in_Bash\"><\/span>Is it possible to display the indices of an indexed array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, it\u2019s possible by simply utilizing the expression <code class=\"\" data-line=\"\">echo ${!my_array[@]}<\/code> to display all the indices of an indexed array in Bash. For instance, to print the indices of the array <strong>arr=(1 2 4)<\/strong>, use <code class=\"\" data-line=\"\">echo ${!arr[@]}<\/code>. The output will be <strong>0 1 2<\/strong>.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"Can I print specific elements of an array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, you can use the array indices with the echo command like this: echo ${array[index_number]} to print the specific element. For instance, to print the 2nd element, use echo ${array[1]}.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the difference between the \u201c${array[@]}\u201d and \u201c${array[*]}\u201d in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Both the expressions with the echo command print all the array items. However, \u201c${array[@]}\u201d treats each item as a separate entity preserving spaces while \u201c${array[*]}\u201d combining all the elements and treating them as a single string.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is a Bash Array?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"A Bash array is a data structure used to store information in an indexed way. The indexed array in Bash has numbered indices while the associative arrays have string indices called keys. Unlike other programming languages, Bash arrays can store elements of various data types.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to print the length of an array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To print the length of an array in Bash, use the echo ${#array[@ or *]} command. It will print the number of elements presented in the array. For example, to print the length of the array num=(1 2 3):<\/p>\n<p>echo ${#num[@]}<\/p>\n<p>The output will be 3.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do I print the key-value pairs of an associative array in a single line?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To print the key-value pairs of an associative array, use the command declare -p followed by the associative array name that you created earlier to fetch the items as well as their corresponding keys. The syntax is: declare -p associative_array_name. However, the keys are printed in random order.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Is it possible to display the indices of an indexed array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes, it\u2019s possible by simply utilizing the expression echo ${!my_array[@]} to display all the indices of an indexed array in Bash. For instance, to print the indices of the array arr=(1 2 4), use echo ${!arr[@]}. The output will be 0 1 2.\"\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\/array\/declare-create-initialize-array\/\" target=\"_blank\" rel=\"noopener\">Declare Array in Bash [Create, Initialize]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/read-array\/\" target=\"_blank\" rel=\"noopener\">How to Read into Bash Array [3 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/index-array\/\" target=\"_blank\" rel=\"noopener\">Index Array in Bash [Explained]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/associative-array\/\" target=\"_blank\" rel=\"noopener\">Bash Associative Arrays [Explained]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/array-of-arrays\/\" target=\"_blank\" rel=\"noopener\">Bash Array of Arrays [Explained]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/elements-of-array\/\" target=\"_blank\" rel=\"noopener\">Elements of Bash Array<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/length-of-array\/\" target=\"_blank\" rel=\"noopener\">How to Get the Length of an Array in Bash?[Complete Guide]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/array-operations\/\" target=\"_blank\" rel=\"noopener\">Array Operations 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;Bash Array&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\/array\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/\" target=\"_blank\" rel=\"noopener\">Bash Array<\/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;26793&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 Print an Array in Bash? [5 Cases]&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>You can use the following 5 methods to print an array in Bash: To print the entire array: ${your_array[@ or &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Print an Array in Bash? [5 Cases]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#more-26793\" aria-label=\"Read more about How to Print an Array in Bash? [5 Cases]\">Read more<\/a><\/p>\n","protected":false},"author":314910,"featured_media":26812,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[178],"class_list":["post-26793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-bash-array","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 Print an Array in Bash? [5 Cases] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"This article discusses 5 different methods on how to print an array in Bash with all the necessary commands and expressions.\" \/>\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\/array\/print-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Print an Array in Bash? [5 Cases] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"This article discusses 5 different methods on how to print an array in Bash with all the necessary commands and expressions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prem.masrur\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-14T05:24:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-23T04:07:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14-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=\"Md Masrur Ul Alam\" \/>\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 Masrur Ul Alam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 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\\\/array\\\/print-array\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/\"},\"author\":{\"name\":\"Md Masrur Ul Alam\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/64033dc86d4cd3b6a549cd0ae9e73423\"},\"headline\":\"How to Print an Array in Bash? [5 Cases]\",\"datePublished\":\"2023-11-14T05:24:46+00:00\",\"dateModified\":\"2024-04-23T04:07:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/\"},\"wordCount\":2339,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/14-feature-image.png\",\"keywords\":[\"bash array\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/\",\"name\":\"How to Print an Array in Bash? [5 Cases] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/14-feature-image.png\",\"datePublished\":\"2023-11-14T05:24:46+00:00\",\"dateModified\":\"2024-04-23T04:07:01+00:00\",\"description\":\"This article discusses 5 different methods on how to print an array in Bash with all the necessary commands and expressions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-array\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/14-feature-image.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/11\\\/14-feature-image.png\",\"width\":400,\"height\":400,\"caption\":\"bash print array\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/print-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\":\"A Complete Guide to Bash Array\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/array\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How to Print an Array in Bash? [5 Cases]\"}]},{\"@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\\\/64033dc86d4cd3b6a549cd0ae9e73423\",\"name\":\"Md Masrur Ul Alam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Masrur-Ul-Alam-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Masrur-Ul-Alam-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Md-Masrur-Ul-Alam-II-96x96.png\",\"caption\":\"Md Masrur Ul Alam\"},\"description\":\"Assalamu Alaikum, I\u2019m Md Masrur Ul Alam, currently working as a Linux OS Content Developer Executive at SOFTEKO. I completed my Bachelor's degree in Electronics and Communication Engineering (ECE)from Khulna University of Engineering &amp; Technology (KUET). With an inquisitive mind, I have always had a keen eye for Science and Technolgy based research and education. I strongly hope this entity leverages the success of my effort in developing engaging yet seasoned content on Linux and contributing to the wealth of technical knowledge. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/prem.masrur\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/masrur-alam\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/masrur\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Print an Array in Bash? [5 Cases] - LinuxSimply","description":"This article discusses 5 different methods on how to print an array in Bash with all the necessary commands and expressions.","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\/array\/print-array\/","og_locale":"en_US","og_type":"article","og_title":"How to Print an Array in Bash? [5 Cases] - LinuxSimply","og_description":"This article discusses 5 different methods on how to print an array in Bash with all the necessary commands and expressions.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/prem.masrur\/","article_published_time":"2023-11-14T05:24:46+00:00","article_modified_time":"2024-04-23T04:07:01+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14-feature-image.png","type":"image\/png"}],"author":"Md Masrur Ul Alam","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Md Masrur Ul Alam","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/"},"author":{"name":"Md Masrur Ul Alam","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/64033dc86d4cd3b6a549cd0ae9e73423"},"headline":"How to Print an Array in Bash? [5 Cases]","datePublished":"2023-11-14T05:24:46+00:00","dateModified":"2024-04-23T04:07:01+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/"},"wordCount":2339,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14-feature-image.png","keywords":["bash array"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/","name":"How to Print an Array in Bash? [5 Cases] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14-feature-image.png","datePublished":"2023-11-14T05:24:46+00:00","dateModified":"2024-04-23T04:07:01+00:00","description":"This article discusses 5 different methods on how to print an array in Bash with all the necessary commands and expressions.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-array\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14-feature-image.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/11\/14-feature-image.png","width":400,"height":400,"caption":"bash print array"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/print-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":"A Complete Guide to Bash Array","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/"},{"@type":"ListItem","position":4,"name":"How to Print an Array in Bash? [5 Cases]"}]},{"@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\/64033dc86d4cd3b6a549cd0ae9e73423","name":"Md Masrur Ul Alam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Masrur-Ul-Alam-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Masrur-Ul-Alam-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Md-Masrur-Ul-Alam-II-96x96.png","caption":"Md Masrur Ul Alam"},"description":"Assalamu Alaikum, I\u2019m Md Masrur Ul Alam, currently working as a Linux OS Content Developer Executive at SOFTEKO. I completed my Bachelor's degree in Electronics and Communication Engineering (ECE)from Khulna University of Engineering &amp; Technology (KUET). With an inquisitive mind, I have always had a keen eye for Science and Technolgy based research and education. I strongly hope this entity leverages the success of my effort in developing engaging yet seasoned content on Linux and contributing to the wealth of technical knowledge. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/prem.masrur\/","https:\/\/www.linkedin.com\/in\/masrur-alam\/"],"url":"https:\/\/linuxsimply.com\/author\/masrur\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/26793","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\/314910"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=26793"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/26793\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/26812"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=26793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=26793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=26793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}