{"id":32778,"date":"2023-12-31T01:39:27","date_gmt":"2023-12-31T01:39:27","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=32778"},"modified":"2024-03-17T04:40:06","modified_gmt":"2024-03-17T04:40:06","slug":"bash-for-loop-list","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/","title":{"rendered":"How to Iterate Through List Using \u201cfor\u201d Loop in Bash"},"content":{"rendered":"<p style=\"text-align: justify;\"><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\"><strong>Loops<\/strong><\/a> are the backbone of task automation in <strong>Bash scripting<\/strong>. They allow your scripts to perform repetitive actions on a set of data, saving you time and effort. Among the various loop constructs available in Bash, the <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\"><strong>for loop<\/strong><\/a> reigns supreme for iterating over lists. Its intuitive syntax and flexibility make it a versatile tool for manipulating data and performing repetitive tasks. This article will explore how you can use the \u201cfor loop\u201d to list words or phrases from the string list in Bash.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/Practice-Files-for-the-List-Iteration-Using-For-Loop-in-Bash-Scripting.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files for the List Iteration Using &#8220;for&#8221; Loop 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\/loop\/for-loop\/for-loop-list\/#8_Cases_of_Using_%E2%80%9Cfor%E2%80%9D_Loop_in_List_Iteration_in_Bash\" >8 Cases of Using \u201cfor\u201d Loop in List Iteration 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-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#1_Iterating_Over_Words_Using_%E2%80%9Cfor%E2%80%9D_Loop\" >1. Iterating Over Words Using \u201cfor\u201d Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#2_Loop_Through_String_List_in_Bash\" >2. Loop Through String List in Bash<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#A_Looping_Through_String_Elements\" >A. Looping Through String Elements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#B_Extract_Words_From_a_Multiword_String_List\" >B. Extract Words From a Multiword String List<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#3_Parameter_Expansion_in_%E2%80%9Cfor%E2%80%9D_Loop\" >3. Parameter Expansion in \u201cfor\u201d Loop<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#A_With_Double_Quotes\" >A. With Double Quotes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#B_Without_Double_Quotes\" >B. Without Double Quotes<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#4_C_Styled_%E2%80%9Cfor%E2%80%9D_Loop_for_String_Iteration_in_Bash\" >4. C Styled \u201cfor\u201d Loop for String Iteration in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#5_Looping_Through_the_String_Using_a_User-defined_Separator\" >5. Looping Through the String Using a User-defined Separator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#6_Reading_String_List_From_File\" >6. Reading String List From File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#7_List_Multiple_Arrays_of_Strings_Together\" >7. List Multiple Arrays of Strings Together<\/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\/loop\/for-loop\/for-loop-list\/#8_Read_the_List_of_Strings_Using_a_Pattern_With_%E2%80%9Cfor%E2%80%9D_Loop\" >8. Read the List of Strings Using a Pattern With \u201cfor\u201d Loop<\/a><\/li><\/ul><\/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\/loop\/for-loop\/for-loop-list\/#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\/loop\/for-loop\/for-loop-list\/#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\/loop\/for-loop\/for-loop-list\/#How_to_iterate_through_a_list_in_a_shell_script\" >How to iterate through a list in a shell script?<\/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\/loop\/for-loop\/for-loop-list\/#Can_you_nest_for_loops_in_Bash\" >Can you nest for loops in Bash?<\/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\/loop\/for-loop\/for-loop-list\/#How_do_I_create_a_list_of_strings_in_Bash\" >How do I create a list 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-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#How_to_print_a_list_in_a_shell_script\" >How to print a list in a shell script?<\/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\/loop\/for-loop\/for-loop-list\/#How_to_iterate_list_items\" >How to iterate list items?<\/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\/loop\/for-loop\/for-loop-list\/#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-22\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#How_do_I_list_all_files_in_the_shell\" >How do I list all files in the shell?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"8_Cases_of_Using_%E2%80%9Cfor%E2%80%9D_Loop_in_List_Iteration_in_Bash\"><\/span>8 Cases of Using \u201cfor\u201d Loop in List Iteration in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In this article, I will go through <strong>8<\/strong> practical cases of utilizing loops with lists, ranging from basic string iteration to advanced techniques like retrieving strings from files, accessing individual words in multi-word elements, and processing multiple arrays simultaneously. These techniques showcase the flexibility of Bash for loops in handling and processing individual words within strings, providing a powerful tool for string manipulation and analysis in your scripts.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Iterating_Over_Words_Using_%E2%80%9Cfor%E2%80%9D_Loop\"><\/span>1. Iterating Over Words Using \u201cfor\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash scripting, the &#8220;for loop&#8221; extends its versatility to efficiently iterate over words within a string. The syntax <code class=\"\" data-line=\"\">for variable in word1 word2 ... wordN<\/code> allows efficient iteration over words in a string. The following two examples illustrate this concept, echoing each word in the provided string:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfor word in Linux is a free, open-source operating system \ndo \necho &quot;Word: $word&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>This script utilizes a &#8220;for loop&#8221; to iterate over each space-separated word in the string \u201cLinux is a free, open-source operating system\u201d. During each iteration, the variable \u2018word\u2019 takes on each word&#8217;s value, and the <a href=\"https:\/\/linuxsimply.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\"><strong>echo command<\/strong><\/a> prints each word with a preset message \u2018Word\u2019 before that. So as a result all word in the sentence is printed out in a separate line.<\/p>\n<\/div><\/div>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nsentence=&quot;Linux is a free, open-source operating system&quot;\n\nfor word in $sentence\ndo\necho &quot;Word: $word&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>This script behaves similarly to the previous example. The only difference is this script uses a variable \u2018sentence\u2019 to store the entire string and afterward in the &#8220;for loop&#8221; the string is split into words based on spaces.<\/p>\n<\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-32789 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/01.-string-iteration.png\" alt=\"Iterating Over Strings Using \u201cfor\u201d Loop in bash\" width=\"825\" height=\"342\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/01.-string-iteration.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/01.-string-iteration-300x124.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/01.-string-iteration-768x318.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">The output shows the string is divided into words using &#8220;for loop&#8221; based on spaces. Each word is echoed with the &#8220;Word:&#8221; prefix.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Loop_Through_String_List_in_Bash\"><\/span>2. Loop Through String List in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Whether you are iterating through individual words in a multi-word string or accessing each string from a string list using a &#8220;for loop&#8221; understanding the syntax is essential for efficient bash scripting. This section will go over those processes using a simple &#8220;for loop&#8221;.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"A_Looping_Through_String_Elements\"><\/span>A. Looping Through String Elements<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">In Bash scripting, the &#8220;for loop&#8221; is a versatile tool. It is used for numeric ranges and iterating over elements within a list of strings. The syntax <code class=\"\" data-line=\"\">for variable in element1 element2 ... elementN<\/code> facilitates seamless iteration. This example demonstrates the effective loop over a list of strings, where each string element is processed individually.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfor str in &quot;Linux&quot; &quot;is a free&quot; &quot;open-source&quot; &quot;operating system&quot;\ndo\necho &quot;String: $str&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>This script uses a &#8220;for loop&#8221; to iterate over a list of strings. The loop processes each string separately, and during each iteration, the variable str represents one of the specified strings. The echo command then prints each string with the prefixed message \u2018String\u2018.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-32790\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/02.-Iterating-over-string-elements-in-a-list.png\" alt=\"Looping Through String Elements in bash scripting\" width=\"825\" height=\"221\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/02.-Iterating-over-string-elements-in-a-list.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/02.-Iterating-over-string-elements-in-a-list-300x80.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/02.-Iterating-over-string-elements-in-a-list-768x206.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">The output shows that all string in the list is echoed in a separate line.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"B_Extract_Words_From_a_Multiword_String_List\"><\/span>B. Extract Words From a Multiword String List<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">In bash scripting, extracting individual words from a list of multi-word strings is a common operation. This task can be easily accomplished by using nested <strong>for<\/strong> loops. The outer loop iterates through each multi-word string in the list, while the inner loop breaks down the string into individual words. The following script gives a proper example of this idea:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfor str in &quot;Linux&quot; &quot;is a free&quot; &quot;open-source&quot; &quot;operating system&quot;\ndo\nfor word in $str\ndo\necho &quot;Word: $word&quot;\ndone\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>This Bash script utilizes nested for loops to iterate over a list of strings and then over each word within those strings. The outer loop iterates over the list of strings, assigning each string to the variable str. The inner loop processes each word within the current string, and the echo command prints each word with the prefixed message \u2018Word\u2019.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-32791\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/03.-individual-words-in-a-list-of-multi-word-strings.png\" alt=\"Extract Words From a Multiword String List using for loop\" width=\"825\" height=\"340\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/03.-individual-words-in-a-list-of-multi-word-strings.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/03.-individual-words-in-a-list-of-multi-word-strings-300x124.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/03.-individual-words-in-a-list-of-multi-word-strings-768x317.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here each word in the list of strings is echoed in a separate line.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Parameter_Expansion_in_%E2%80%9Cfor%E2%80%9D_Loop\"><\/span>3. Parameter Expansion in \u201cfor\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/expansion\/parameter-expansion\/\" target=\"_blank\" rel=\"noopener\"><strong>Parameter expansion<\/strong><\/a> in Bash refers to the process of manipulating or extracting values from variables by using special symbols or operators. It allows you to perform various operations on the values stored in variables. The common syntax for parameter expansion is <code class=\"\" data-line=\"\">${variable}<\/code>, where the variable is the name of the variable you want to expand.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"A_With_Double_Quotes\"><\/span>A. With Double Quotes<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">The <strong>special index variable<\/strong> \u201c@\u201d provides a convenient way to iterate over elements within a list. This parameter expansion technique ensures the correct way of handling spaces in a multi-word phrase. Using the syntax <code class=\"\" data-line=\"\">for variable in \u201c${array[@]}\u201d<\/code> the following example demonstrates a seamless iteration over a list of string phrases. The usage of <strong>double quotes<\/strong> is <strong>necessary<\/strong> to preserve the integrity of each <strong>string phrase<\/strong>.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstringPhrases=(&quot;Linux&quot; &quot;is a free&quot; &quot;open-source&quot; &quot;operating system&quot;)\n\nfor str in &quot;${stringPhrases[@]}&quot; # or you can use \u201c${stringPhrases[*]}\u201d\ndo\necho &quot;String: $str&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>This script initializes an array stringPhrases with four elements, each containing a different string. The &#8220;for loop&#8221; iterates over the elements of the array, assigning each string to the variable str. During each iteration, the echo command prints each string with the prefixed message \u2018String\u2019. Parameter expansion is used using [@] to ensure that each element of the array is treated as a separate item during iteration.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-32792 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/04.-parameter-expansion-With-double-quotes.png\" alt=\"Parameter Expansion With Double Quotes\" width=\"825\" height=\"231\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/04.-parameter-expansion-With-double-quotes.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/04.-parameter-expansion-With-double-quotes-300x84.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/04.-parameter-expansion-With-double-quotes-768x215.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\"><strong>With double quotes<\/strong>, parameter expansion echoes <strong>each string in a separate line<\/strong>.<\/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;\"><strong>Note<\/strong>: <code class=\"\" data-line=\"\">${stringPhrases[*]}<\/code> works in a <strong>similar manner<\/strong> as <code class=\"\" data-line=\"\">${stringPhrases[@]}<\/code>. So you can use <code class=\"\" data-line=\"\">for str in &quot;${stringPhrases[*]}&quot;<\/code> instead of <code class=\"\" data-line=\"\">for str in &quot;${stringPhrases[@]}&quot;<\/code>\u00a0but in general using the later one is <strong>recommended<\/strong>.<\/div><\/div>\n<h4><span class=\"ez-toc-section\" id=\"B_Without_Double_Quotes\"><\/span>B. Without Double Quotes<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\">In Bash scripting, parameter expansion <strong>without double quotes<\/strong> offers a method to <strong>extract individual words<\/strong> from a list of multi-word strings. Using the syntax <code class=\"\" data-line=\"\">for variable in ${array[@]}<\/code>\u00a0will allow the straightforward extraction and processing of individual words. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstringPhrases=(&quot;Linux&quot; &quot;is a free&quot; &quot;open-source&quot; &quot;operating system&quot;)\n\nfor word in ${stringPhrases[@]} # or you can use ${stringPhrases[*]}\ndo\necho &quot;Word: $word&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>This Bash script begins by creating a stringPhrases array with four different strings. It then uses a &#8220;for loop&#8221; to go through each string in the array, treating each one as a word. The variable word takes on the value of each string during the loop, and the script prints out each word with the label &#8220;Word&#8221;. Using [@] ensures that each string in the array is considered separately during the iteration.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-32793 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/05.-parameter-expansion-Without-double-quotes.png\" alt=\"Parameter Expansion Without Double Quotes\" width=\"825\" height=\"338\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/05.-parameter-expansion-Without-double-quotes.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/05.-parameter-expansion-Without-double-quotes-300x123.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/05.-parameter-expansion-Without-double-quotes-768x315.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Without double quotes, parameter expansion echoes each word of each string in a separate line.<\/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;\"><strong>Note<\/strong>: You can also declare an array using <code class=\"\" data-line=\"\">declare -a stringArr=(&quot;Using&quot; &quot;for&quot; &quot;loops&quot; &quot;for&quot; &quot;string iteration&quot;)<\/code>. But for simple loop iteration, it is not necessary.<\/div><\/div>\n<h3><span class=\"ez-toc-section\" id=\"4_C_Styled_%E2%80%9Cfor%E2%80%9D_Loop_for_String_Iteration_in_Bash\"><\/span>4. C Styled \u201cfor\u201d Loop for String Iteration in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash scripting, the <strong>C-style &#8220;for loop&#8221;<\/strong> presents a structured and familiar approach to iteration. This technique proves useful when dealing with arrays, providing explicit control over the <strong>loop&#8217;s initialization, condition, and iteration expressions<\/strong>. The following example uses a C-style &#8220;for loop&#8221; to iterate through the stringPhrases array:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstringPhrases=(&quot;Linux&quot; &quot;is a free&quot; &quot;open-source&quot; &quot;operating system&quot;)\n\nlen=${#stringPhrases[@]}\n\nfor((i=0; i&lt;$len; i++))\ndo\necho &quot;String: ${stringPhrases[$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>This script ensures individual processing of each string in the array using a C-styled &#8220;for loop&#8221;. It starts by forming an array named stringPhrases with four distinct strings. The length of the array is determined using the variable len. Then it uses a C-style &#8220;for loop&#8221; to iterate through each element of the array using the index variable i. Within each iteration, the script prints the current string with the label &#8220;String&#8221;.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-32794\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/06.-c-style-for-loop.png\" alt=\"C Styled \u201cfor\u201d Loop for String Iteration in Bash scripting\" width=\"825\" height=\"233\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/06.-c-style-for-loop.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/06.-c-style-for-loop-300x85.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/06.-c-style-for-loop-768x217.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">This C-styled &#8220;for loop&#8221; echoes the string from the list.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Looping_Through_the_String_Using_a_User-defined_Separator\"><\/span>5. Looping Through the String Using a User-defined Separator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash scripting, iterating through a string with a user-defined separator involves breaking the string into segments using a specified delimiter. <strong>Internal Field Separator (IFS)<\/strong> is a special variable used to determine the delimiter that separates the words in a string. By <strong>default<\/strong>, IFS is set to <strong>whitespace (space, tab, and newline)<\/strong>, but it can be customized to any character or set of characters to define the boundaries between strings.<\/p>\n<p style=\"text-align: justify;\">In the following example, a custom delimiter is used to iterate through the \u2018sentence\u2019 string:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nsentence=&quot;Linux-is a free-open-source-operating system&quot;\n\nDEFAULT_IFS=&quot;$IFS&quot;\nIFS=&#039;-&#039;\n\nfor str in $sentence\ndo\necho &quot;String: $str&quot;\ndone\n\nIFS=$DEFAULT_IFS<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>This script effectively processes and prints each word in the <strong>hyphen-separated string<\/strong>.<\/p>\n<p>It starts by defining a string named sentence containing hyphen-separated words. It <strong>temporarily changes<\/strong> the Internal Field Separator (<strong>IFS<\/strong>) to the <strong>hyphen(-)<\/strong>. Then, using a &#8220;for loop&#8221;, it iterates through the words in the sentence. During each iteration, it prints the current word with the label &#8220;String&#8221;. After the loop, it <strong>restores<\/strong> the <strong>original IFS<\/strong>.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-32795\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/07.-Iteration-over-the-string-using-a-custom-delimiter.png\" alt=\"Looping Through the String Using a User-defined Separator\" width=\"825\" height=\"266\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/07.-Iteration-over-the-string-using-a-custom-delimiter.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/07.-Iteration-over-the-string-using-a-custom-delimiter-300x97.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/07.-Iteration-over-the-string-using-a-custom-delimiter-768x248.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, the main string is divided into small pieces based on the hyphen(-) location.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Reading_String_List_From_File\"><\/span>6. Reading String List From File<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">While working with bash scripting there may be cases when you need to <strong>extract data<\/strong> from a <strong>file<\/strong>. The following example demonstrates the process of fetching a list of strings from a file named <strong>list.txt<\/strong>. Each line in the file represents a distinct string, and a &#8220;for loop&#8221; is used to iterate through these strings.<\/p>\n<p>list.txt file contains the following text :<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">Linux\nis a free\nopen-source\noperating system<\/code><\/pre>\n<p>Bash script to read the strings from a file:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# File containing the list of strings\nfile=&quot;list.txt&quot;\n\nfor line in $(cat &quot;$file&quot;)\ndo\necho &quot;Word: $line&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>This script reads and displays each string from the specified file. It starts by specifying a file named &#8220;list.txt&#8221; containing a list of strings. It then uses a &#8220;for loop&#8221; to iterate through each line in the file. During each iteration, the script echoes the current line with the label &#8220;Word&#8221;.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-32796\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/08.-Retrieving-List-of-Strings-From-a-File.png\" alt=\"Reading String List From File using for loop\" width=\"825\" height=\"333\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/08.-Retrieving-List-of-Strings-From-a-File.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/08.-Retrieving-List-of-Strings-From-a-File-300x121.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/08.-Retrieving-List-of-Strings-From-a-File-768x310.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, from the text file, each word is echoed in a separate line.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"7_List_Multiple_Arrays_of_Strings_Together\"><\/span>7. List Multiple Arrays of Strings Together<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash scripting, there are scenarios where you may need to read and process multiple string arrays simultaneously. The following example demonstrates the task of combining two string arrays, <code class=\"\" data-line=\"\">stringPhrases_1<\/code> and <code class=\"\" data-line=\"\">stringPhrases_2<\/code>, into a single array named <code class=\"\" data-line=\"\">combinedStringPhrases<\/code>. Then, a <strong>nested &#8220;for loop&#8221;<\/strong> is used to iterate through the <strong>combined array<\/strong>, extracting and echoing each string:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nstringPhrases_1=(&quot;Linux&quot; &quot;is a free&quot;)\nstringPhrases_2=(&quot;open-source&quot; &quot;operating system&quot;)\n\ncombinedStringPhrases=(&quot;${stringPhrases_1[@]}&quot; &quot;${stringPhrases_2[@]}&quot;)\n\nfor phrase in &quot;${combinedStringPhrases[@]}&quot;; do\nfor str in &quot;${!phrase[@]}&quot;; do\necho &quot;String: ${phrase[$str]}&quot;\ndone\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>This script reads and prints each string character by character from the combined arrays. It starts by creating two arrays, stringPhrases_1 and stringPhrases_2, each containing distinct strings. It then combines these arrays into a new array named combinedStringPhrases. Using nested for loops, the script iterates through each element in the combined array, treating each element as a phrase. Within each phrase, it iterates through individual characters, printing each character with the label &#8220;String&#8221;.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-32797\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/09.-Reading-multiple-string-arrays-together.png\" alt=\"List Multiple Arrays of Strings Together using for loop in bash scripting\" width=\"825\" height=\"230\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/09.-Reading-multiple-string-arrays-together.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/09.-Reading-multiple-string-arrays-together-300x84.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/09.-Reading-multiple-string-arrays-together-768x214.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, after merging 2 arrays each string element of the merged array is printed out.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"8_Read_the_List_of_Strings_Using_a_Pattern_With_%E2%80%9Cfor%E2%80%9D_Loop\"><\/span>8. Read the List of Strings Using a Pattern With \u201cfor\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Pattern recognition<\/strong> and manipulation is a powerful technique to extract information from a string. The following example showcases this technique. It takes a hyphen-separated string as input and uses a &#8220;for loop&#8221; to break it into individual words.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nsentence=&quot;Linux-is a free-open-source-operating system&quot;\n\nfor str in ${sentence\/\/-\/ }\ndo\necho &quot;Word: $str&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>This Bash script processes and prints each word in the hyphen-separated string after replacing hyphens with spaces. It starts with a string named &#8220;sentence&#8221; containing hyphen-separated words. Using a &#8220;for loop&#8221;, it replaces hyphens with spaces in the string and then iterates through each word. During each iteration, the script echoes the current word with the label &#8220;Word&#8221;.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-32798\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/10.-Using-pattern-to-read-the-list-of-strings.png\" alt=\"Read the List of Strings Using a Pattern With \u201cfor\u201d Loop\" width=\"825\" height=\"371\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/10.-Using-pattern-to-read-the-list-of-strings.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/10.-Using-pattern-to-read-the-list-of-strings-300x135.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/10.-Using-pattern-to-read-the-list-of-strings-768x345.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Here, every word in the string is echoed in a separate line.<\/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;\">Mastering the &#8220;for loop&#8221; in Bash elevates your scripting skills, giving you the precision to manipulate strings with ease. Whether parsing files, iterating through arrays, or employing custom delimiters, the &#8220;for loop&#8221; proves indispensable. Its simple yet adaptable nature makes it an essential tool for any Bash script. I hope you will embrace the power of the &#8220;for loop&#8221; and use it in bash 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=\"How_to_iterate_through_a_list_in_a_shell_script\"><\/span>How to iterate through a list in a shell script?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To <strong>iterate through a list<\/strong> in a shell script, you can use various methods, and the choice depends on the <strong>nature of your list<\/strong>. Here is a very simple example using a \u201c<strong>for loop<\/strong>\u201d with space-separated list elements:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nmyList=&quot;item1 item2 item3 item4&quot;\nfor item in $myList\ndo\necho &quot;Current item: $item&quot;\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">This script initializes a space-separated list called myList and iterates through its elements using a &#8220;for loop&#8221;. During each iteration, it prints the current item as &#8220;Current item: item1&#8221;, &#8220;Current item: item2&#8221;, and so forth for each element in the list.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_you_nest_for_loops_in_Bash\"><\/span>Can you nest for loops in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong>, you can nest for loops in Bash. Nesting means placing one or more loops inside another loop. This allows you to <strong>create<\/strong> more <strong>complex loop structures<\/strong> for iterating through multiple data sets. Here&#8217;s a simple example of nested for loops:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfor i in {1..3}; do\necho &quot;Outer loop iteration $i&quot;\nfor j in {A..C}; do\necho &quot;\u00a0\u00a0\u00a0 Inner loop iteration $j&quot;\ndone\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">In this example, there&#8217;s an outer loop that iterates three times, and for each iteration of the outer loop, there&#8217;s an inner loop that iterates through the letters A to C. The output will show the iterations of both the outer and inner loops.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_create_a_list_of_strings_in_Bash\"><\/span>How do I create a list of strings in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To create a list of strings you can either use <strong>space-separated elements<\/strong> or an <strong>array<\/strong>. To create a list of strings using <strong>space-separated elements<\/strong> use <code class=\"\" data-line=\"\">listVariable=&quot;element1 element2 element3&quot;<\/code>. And to create a <strong>list of strings using an array<\/strong> use <code class=\"\" data-line=\"\">arrayName=(&quot;element1&quot; &quot;element2&quot; &quot;element3&quot;)<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_print_a_list_in_a_shell_script\"><\/span>How to print a list in a shell script?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To print a <strong>list in a shell script<\/strong>, you can use a simple &#8220;for loop&#8221;. Here&#8217;s an example script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define a list\nmyList=(&quot;apple&quot; &quot;banana&quot; &quot;orange&quot; &quot;cherry&quot;)\n\n# Print each item in the list\nfor item in &quot;${myList[@]}&quot;; do\necho &quot;$item&quot;\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">This script initializes an array called myList with four elements and then uses a &#8220;for loop&#8221; to iterate through the list, printing each item on a new line.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_iterate_list_items\"><\/span>How to iterate list items?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">There are many ways to iterate list items. Here&#8217;s an example of iterating through a list in Bash using a \u201c<strong>for loop<\/strong>\u201d:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define a list\nmyList=&quot;apple banana orange cherry&quot;\n\n# Use a for loop to iterate through the list\nfor item in $myList\ndo\necho &quot;Current item: $item&quot;\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">This Bash script initializes a space-separated list called myList, containing elements &#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;orange&#8221;, and &#8220;cherry&#8221;. It then uses a &#8220;for loop&#8221; to iterate through each item in the list and echoes that item.<\/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 <strong>data structure<\/strong> that stores a collection of elements. It allows grouping multiple values under a single variable name. Arrays can hold <strong>various types of data<\/strong>. Elements are accessed using<strong> indices<\/strong>, and arrays offer flexibility for managing and manipulating data in Bash scripts. Here&#8217;s a simple example of a Bash array:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define an array\nmyArray=(&quot;apple&quot; &quot;banana&quot; &quot;orange&quot; &quot;cherry&quot;)\n\n# Access and print array elements\necho &quot;First element: ${myArray[0]}&quot;\necho &quot;Second element: ${myArray[1]}&quot;\necho &quot;All elements: ${myArray[@]}&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\">This Bash script defines an array named myArray with four elements. It then accesses and prints the first and second elements individually, as well as all elements collectively, demonstrating basic array usage in Bash.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_list_all_files_in_the_shell\"><\/span>How do I list all files in the shell?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To <strong>list all files<\/strong> in the shell, you can use the <code class=\"\" data-line=\"\">ls<\/code> command with different options. Type the <code class=\"\" data-line=\"\">ls<\/code>\u00a0command for a <strong>basic listing<\/strong> in the <strong>current directory<\/strong>. For detailed information, including <strong>file permissions and sizes<\/strong>, use <code class=\"\" data-line=\"\">ls -l<\/code>\u00a0command. To list all files including the <strong>hidden files<\/strong> use <code class=\"\" data-line=\"\">ls -a<\/code>. To list the <strong>files recursively<\/strong>, use <code class=\"\" data-line=\"\">ls -R<\/code>. You can also <strong>merge multiple options<\/strong> together.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"How do I create a list of strings in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To create a list of strings you can either use space-separated elements or an array. To create a list of strings using space-separated elements use listVariable=\\\"element1 element2 element3\\\". And to create a list of strings using an array use arrayName=(\\\"element1\\\" \\\"element2\\\" \\\"element3\\\").\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do I list all files in the shell?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To list all files in the shell, you can use the ls command with different options. Type the ls\u00a0command for a basic listing in the current directory. For detailed information, including file permissions and sizes, use ls -l\u00a0command. To list all files including the hidden files use ls -a. To list the files recursively, use ls -R. You can also merge multiple options together.\"\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\/loop\/for-loop\/for-loop-example\/\" target=\"_blank\" rel=\"noopener\">10 Common Bash \u201cfor\u201d Loop Examples [Basic to Intermediate]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-with-variable\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash \u201cfor\u201d Loop with Variable [12 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/increment-variable\/\" target=\"_blank\" rel=\"noopener\">Bash Increment and Decrement Variable in \u201cfor\u201d Loop<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/parallel-for-loop\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash Parallel \u201cfor\u201d Loop [7 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-array\/\" target=\"_blank\" rel=\"noopener\">How to Loop Through Array Using \u201cfor\u201d Loop in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/range\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash \u201cfor\u201d Loop with Range [5 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/bash-seq\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash \u201cfor\u201d Loop with \u201cseq\u201d Command [10 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/files\/\" target=\"_blank\" rel=\"noopener\">How to Use &#8220;for&#8221; Loop in Bash Files [9 Practical Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/directory\/\" target=\"_blank\" rel=\"noopener\">Usage of \u201cfor\u201d Loop in Bash Directory [5 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/one-line\/\" target=\"_blank\" rel=\"noopener\">How to Use \u201cfor\u201d Loop in One Line in Bash [7 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/nested-for-loop\/\" target=\"_blank\" rel=\"noopener\">How to Use Nested \u201cfor\u201d Loop in Bash [7 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/infinite-loop\/\" target=\"_blank\" rel=\"noopener\">How to Create Infinite Loop in Bash [7 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-continue\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash Continue with \u201cfor\u201d Loop [9 Examples]<\/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;For Loop 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\/loop\/for-loop\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\">For Loop in Bash<\/a> |\u00a0<\/span><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Loops 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\/loop\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\">Loops in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span><\/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;32778&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;5&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&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;5\\\/5 - (5 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Iterate Through List Using \u201cfor\u201d Loop in Bash&quot;,&quot;width&quot;:&quot;142.5&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: 142.5px;\">\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            5\/5 - (5 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Loops are the backbone of task automation in Bash scripting. They allow your scripts to perform repetitive actions on a &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Iterate Through List Using \u201cfor\u201d Loop in Bash\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#more-32778\" aria-label=\"Read more about How to Iterate Through List Using \u201cfor\u201d Loop in Bash\">Read more<\/a><\/p>\n","protected":false},"author":314909,"featured_media":32799,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[175],"class_list":["post-32778","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-for-loop","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 Iterate Through List Using \u201cfor\u201d Loop in Bash - LinuxSimply<\/title>\n<meta name=\"description\" content=\"In bash scripting, use the for loop to iterate through a list of strings or a string array or iterate through the string from a text file.\" \/>\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\/loop\/for-loop\/for-loop-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Iterate Through List Using \u201cfor\u201d Loop in Bash - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"In bash scripting, use the for loop to iterate through a list of strings or a string array or iterate through the string from a text file.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/ridoychandra.shil.9\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-31T01:39:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-17T04:40:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/bash-for-loop-list-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=\"Ridoy Chandra Shil\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ridoy Chandra Shil\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 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\\\/loop\\\/for-loop\\\/for-loop-list\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/\"},\"author\":{\"name\":\"Ridoy Chandra Shil\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/94793d6ad5334004f864932ed89e58b4\"},\"headline\":\"How to Iterate Through List Using \u201cfor\u201d Loop in Bash\",\"datePublished\":\"2023-12-31T01:39:27+00:00\",\"dateModified\":\"2024-03-17T04:40:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/\"},\"wordCount\":2672,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/bash-for-loop-list-feature-image.png\",\"keywords\":[\"for loop\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/\",\"name\":\"How to Iterate Through List Using \u201cfor\u201d Loop in Bash - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/bash-for-loop-list-feature-image.png\",\"datePublished\":\"2023-12-31T01:39:27+00:00\",\"dateModified\":\"2024-03-17T04:40:06+00:00\",\"description\":\"In bash scripting, use the for loop to iterate through a list of strings or a string array or iterate through the string from a text file.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/bash-for-loop-list-feature-image.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/bash-for-loop-list-feature-image.png\",\"width\":400,\"height\":400,\"caption\":\"Use the for loop In bash scripting to iterate through a list of strings or a string array or iterate through the string from a text file.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/for-loop-list\\\/#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\":\"Loops in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"For Loop in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/for-loop\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Iterate Through List Using \u201cfor\u201d Loop in Bash\"}]},{\"@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\\\/94793d6ad5334004f864932ed89e58b4\",\"name\":\"Ridoy Chandra Shil\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Ridoy-Chandra-Shil-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Ridoy-Chandra-Shil-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Ridoy-Chandra-Shil-II-96x96.png\",\"caption\":\"Ridoy Chandra Shil\"},\"description\":\"Hello everyone. I am Ridoy Chandra Shil, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Biomedical Engineering graduate from Bangladesh University of Engineering and Technology. I am a science and tech enthusiast. In my free time, I read articles about new tech and watch documentaries on science-related topics. I am also a big fan of \u201cThe Big Bang Theory\u201d. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/ridoychandra.shil.9\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/ridoy-chandra-shil\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/ridoy\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Iterate Through List Using \u201cfor\u201d Loop in Bash - LinuxSimply","description":"In bash scripting, use the for loop to iterate through a list of strings or a string array or iterate through the string from a text file.","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\/loop\/for-loop\/for-loop-list\/","og_locale":"en_US","og_type":"article","og_title":"How to Iterate Through List Using \u201cfor\u201d Loop in Bash - LinuxSimply","og_description":"In bash scripting, use the for loop to iterate through a list of strings or a string array or iterate through the string from a text file.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/ridoychandra.shil.9","article_published_time":"2023-12-31T01:39:27+00:00","article_modified_time":"2024-03-17T04:40:06+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/bash-for-loop-list-feature-image.png","type":"image\/png"}],"author":"Ridoy Chandra Shil","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ridoy Chandra Shil","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/"},"author":{"name":"Ridoy Chandra Shil","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/94793d6ad5334004f864932ed89e58b4"},"headline":"How to Iterate Through List Using \u201cfor\u201d Loop in Bash","datePublished":"2023-12-31T01:39:27+00:00","dateModified":"2024-03-17T04:40:06+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/"},"wordCount":2672,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/bash-for-loop-list-feature-image.png","keywords":["for loop"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/","name":"How to Iterate Through List Using \u201cfor\u201d Loop in Bash - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/bash-for-loop-list-feature-image.png","datePublished":"2023-12-31T01:39:27+00:00","dateModified":"2024-03-17T04:40:06+00:00","description":"In bash scripting, use the for loop to iterate through a list of strings or a string array or iterate through the string from a text file.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/bash-for-loop-list-feature-image.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/bash-for-loop-list-feature-image.png","width":400,"height":400,"caption":"Use the for loop In bash scripting to iterate through a list of strings or a string array or iterate through the string from a text file."},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/for-loop-list\/#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":"Loops in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/"},{"@type":"ListItem","position":4,"name":"For Loop in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/"},{"@type":"ListItem","position":5,"name":"How to Iterate Through List Using \u201cfor\u201d Loop in Bash"}]},{"@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\/94793d6ad5334004f864932ed89e58b4","name":"Ridoy Chandra Shil","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Ridoy-Chandra-Shil-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Ridoy-Chandra-Shil-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Ridoy-Chandra-Shil-II-96x96.png","caption":"Ridoy Chandra Shil"},"description":"Hello everyone. I am Ridoy Chandra Shil, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Biomedical Engineering graduate from Bangladesh University of Engineering and Technology. I am a science and tech enthusiast. In my free time, I read articles about new tech and watch documentaries on science-related topics. I am also a big fan of \u201cThe Big Bang Theory\u201d. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/ridoychandra.shil.9","https:\/\/www.linkedin.com\/in\/ridoy-chandra-shil\/"],"url":"https:\/\/linuxsimply.com\/author\/ridoy\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/32778","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\/314909"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=32778"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/32778\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/32799"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=32778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=32778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=32778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}