{"id":2945,"date":"2023-08-05T18:50:53","date_gmt":"2023-08-05T13:20:53","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2945"},"modified":"2023-08-17T15:09:59","modified_gmt":"2023-08-17T09:39:59","slug":"how-to-use-loops-in-bash","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/","title":{"rendered":"How to Use Loops in Bash?"},"content":{"rendered":"\n<p>Bash scripting offers a variety of loops that allow you to repeatedly execute a sequence of commands. Whether you&#8217;re a beginner in bash scripting or an experienced developer, understanding how to use loops effectively is a fundamental skill. In this comprehensive guide, we will explore different types of loops in bash, their applications, and provide examples to help you master the art of loop scripting.<\/p>\n\n\n\n<p><a href=\"https:\/\/smarttech101.com\/category\/bash\/\" target=\"_blank\" rel=\"noreferrer noopener\">This is part of my series on bash. Look at all the articles over here<\/a>.<\/p>\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 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 \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Introduction_to_Loops_in_Bash\" title=\"Introduction to Loops in Bash\">Introduction to Loops in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#The_Basic_For_Loop\" title=\"The Basic For Loop\">The Basic For Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Looping_Through_Arrays\" title=\"Looping Through Arrays\">Looping Through Arrays<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#The_Versatile_While_Loop\" title=\"The Versatile While Loop\">The Versatile While Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#The_Purpose_of_the_Until_Loops_in_bash\" title=\"The Purpose of the Until Loops in bash\">The Purpose of the Until Loops in bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Iterating_Through_Files_with_Loops\" title=\"Iterating Through Files with Loops\">Iterating Through Files with Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Adding_Incremental_Steps_to_Loops_in_bash\" title=\"Adding Incremental Steps to Loops in bash\">Adding Incremental Steps to Loops in bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Looping_Through_Command_Output\" title=\"Looping Through Command Output\">Looping Through Command Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Implementing_Conditional_Loops_in_bash\" title=\"Implementing Conditional Loops in bash\">Implementing Conditional Loops in bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Skipping_Iterations_with_the_continue_Statement\" title=\"Skipping Iterations with the continue Statement\">Skipping Iterations with the continue Statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Breaking_Out_of_Loops_with_the_break_Statement_in_bash\" title=\"Breaking Out of Loops with the break Statement in bash\">Breaking Out of Loops with the break Statement in bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Looping_through_File_content\" title=\"Looping through File content\">Looping through File content<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Expanding_Horizons_Looping_Through_Multiple_Ranges\" title=\"Expanding Horizons: Looping Through Multiple Ranges\">Expanding Horizons: Looping Through Multiple Ranges<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Creative_Usage_Creating_a_Timer_with_Loops\" title=\"Creative Usage: Creating a Timer with Loops\">Creative Usage: Creating a Timer with Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction_to_Loops_in_Bash\"><\/span>Introduction to Loops in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Loops form an essential part of bash scripting, allowing you to perform repetitive tasks efficiently. They enable you to iterate through lists, arrays, files, and even command outputs, executing a set of instructions for each iteration. Bash provides several types of loops, each tailored to specific scenarios.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Basic_For_Loop\"><\/span>The Basic For Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The basic <code>for<\/code> loop is your starting point in loop scripting. It iterates over a range of values and performs actions for each value. Consider the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in {1..5}; do\n    echo \"Number: $i\"\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number: 1\nNumber: 2\nNumber: 3\nNumber: 4\nNumber: 5<\/code><\/pre>\n\n\n\n<p>In this loop, the variable <strong><code>i<\/code><\/strong> takes values from 1 to 5, and the <code>echo<\/code> command displays each number along with the descriptive text &#8220;Number: &#8220;. This type of loop is particularly useful when you need to execute a specific action a predetermined number of times.<\/p>\n\n\n\n<p><a href=\"https:\/\/smarttech101.com\/understanding-brace-expansion-in-bash-generating-sequences\/\" target=\"_blank\" rel=\"noreferrer noopener\">To learn about braces expansion in bash ({}). Look at this article<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Looping_Through_Arrays\"><\/span>Looping Through Arrays<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Arrays are fundamental data structures in bash, and you can use loops to iterate through their elements. Take a look at this example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits=(\"apple\" \"banana\" \"orange\")\nfor fruit in \"${fruits&#091;@]}\"; do\n    echo \"Fruit: $fruit\"\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Fruit: apple\nFruit: banana\nFruit: orange<\/code><\/pre>\n\n\n\n<p>This loop cycles through the elements of the <strong><code>fruits<\/code><\/strong> array, displaying each fruit&#8217;s name along with &#8220;Fruit: &#8220;. This approach is beneficial when you want to process each element in an array individually.<\/p>\n\n\n\n<p><strong>Note: <\/strong>Here <code>echo<\/code> command is being used to print on terminal. <a href=\"https:\/\/smarttech101.com\/echo-with-examples\/\" target=\"_blank\" rel=\"noreferrer noopener\">Learn more about echo in detail in this article<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Versatile_While_Loop\"><\/span>The Versatile While Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong><code>while<\/code> loop<\/strong> continuously executes a block of code as long as a specific condition remains true. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>count=1\nwhile &#091; $count -le 3 ]; do\n    echo \"Count: $count\"\n    count=$((count+1))\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Count: 1\nCount: 2\nCount: 3<\/code><\/pre>\n\n\n\n<p>In this instance, the loop prints the value of <strong><code>count<\/code><\/strong> as long as it is less than or equal to 3. The <strong><code>while<\/code> loop<\/strong> is ideal for scenarios where the number of iterations is not predetermined.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Purpose_of_the_Until_Loops_in_bash\"><\/span>The Purpose of the Until Loops in bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong><code>until<\/code> loop<\/strong> is the counterpart of the <strong><code>while<\/code> loop<\/strong>; it continues executing until a given condition becomes true. Consider this illustration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num=0\nuntil &#091; $num -ge 5 ]; do\n    echo \"Number: $num\"\n    num=$((num+1))\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number: 0\nNumber: 1\nNumber: 2\nNumber: 3\nNumber: 4<\/code><\/pre>\n\n\n\n<p>Here, the loop prints the value of <strong><code>num<\/code><\/strong> until it becomes greater than or equal to 5. The <strong><code>until<\/code> loop<\/strong> is advantageous when you want to repeat a task until a specific criterion is met.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Iterating_Through_Files_with_Loops\"><\/span>Iterating Through Files with Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Looping through files is a common scenario in bash scripting. The following example showcases this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in '\/home\/ajay\/Videos\/OP\/1063.mkv' '\/home\/ajay\/Videos\/OP\/1064.mkv' '\/home\/ajay\/Videos\/OP\/1065.mkv'\ndo\n  ffmpeg -i \"$i\" -codec:v libx265 \"${i}_x265.mkv\"\ndone<\/code><\/pre>\n\n\n\n<p>This loop compresses each video file using ffmpeg*. It&#8217;s a handy technique when you need to perform batch operations on files.<\/p>\n\n\n\n<p><a href=\"https:\/\/smarttech101.com\/ffmpeg-compress-rescale-video-and-image\/\" target=\"_blank\" rel=\"noreferrer noopener\">To learn more about ffmpeg compression of videos and images, look at this article<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Adding_Incremental_Steps_to_Loops_in_bash\"><\/span>Adding Incremental Steps to Loops in bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Loops can include <strong>incremental steps<\/strong> to control the iteration process. Observe this example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for ((i=0; i&lt;10; i+=2)); do\n    echo \"Step: $i\"\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Step: 0\nStep: 2\nStep: 4\nStep: 6\nStep: 8<\/code><\/pre>\n\n\n\n<p>Here, the loop iterates through numbers starting from 0, increasing by 2 in each iteration. This feature is valuable for scenarios where you need to process every nth element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Looping_Through_Command_Output\"><\/span>Looping Through Command Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Looping through the output of commands is another valuable capability of bash loops. Consider the following script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for file in $(ls *.txt)\ndo\n    echo \"Text File: $file\"\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Text File: ascii.txt\nText File: file_1.txt\nText File: file1.txt\nText File: file_2.txt\nText File: file_3.txt\nText File: file_4.txt\nText File: file_5.txt\nText File: file.txt\nText File: romeo.txt\nText File: sample.txt\nText File: test.txt\nText File: unicode.txt\nText File: utf8.txt<\/code><\/pre>\n\n\n\n<p>In this example, the loop goes through the output of the command <code>ls *.txt<\/code> and prints them. This technique is useful when you want to process data generated by a command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Implementing_Conditional_Loops_in_bash\"><\/span>Implementing Conditional Loops in bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Conditional loops combine loops and conditional statements to execute commands based on specific criteria. Take a look at this illustration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for num in {1..5}; do\n    if &#091; $num -gt 2 ]; then\n        echo \"Number: $num\"\n    fi\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number: 3\nNumber: 4\nNumber: 5<\/code><\/pre>\n\n\n\n<p>This loop prints numbers greater than 2. It&#8217;s an effective approach when you need to filter elements during iteration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Skipping_Iterations_with_the_continue_Statement\"><\/span>Skipping Iterations with the <strong><code>continue<\/code><\/strong> Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong><code>continue<\/code><\/strong> statement allows you to skip iterations within a loop based on certain conditions. Explore this example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for num in {1..5}; do\n    if &#091; $num -eq 3 ]; then\n        continue\n    fi\n    echo \"Number: $num\"\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number: 1\nNumber: 2\nNumber: 4\nNumber: 5<\/code><\/pre>\n\n\n\n<p>In this loop, the iteration with the value 3 is skipped due to the <strong><code>continue<\/code><\/strong> statement. This feature is handy for excluding specific cases from processing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Breaking_Out_of_Loops_with_the_break_Statement_in_bash\"><\/span>Breaking Out of Loops with the <strong><code>break<\/code><\/strong> Statement in bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong><code>break<\/code><\/strong> statement provides a way to prematurely exit a loop based on certain conditions. Consider this example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for num in {1..5}; do\n    if &#091; $num -eq 4 ]; then\n        break\n    fi\n    echo \"Number: $num\"\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number: 1\nNumber: 2\nNumber: 3<\/code><\/pre>\n\n\n\n<p>The loop terminates when the value of <strong><code>num<\/code><\/strong> becomes 4. The <strong><code>break<\/code><\/strong> statement is useful for stopping execution once a particular condition is met.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Looping_through_File_content\"><\/span>Looping through File content<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Bash loops can also operate on data from files. The following script showcases this technique.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;ajay@legion Pictures]$ cat numbers.txt\n5\n8\n3\n9\n2<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>while read -r number; do\n    echo \"Number: $number\"\ndone &lt; numbers.txt<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number: 5\nNumber: 8\nNumber: 3\nNumber: 9\nNumber: 2<\/code><\/pre>\n\n\n\n<p>Here, the loop reads numbers from the <strong><code>numbers.txt<\/code><\/strong> file and prints them using <code>echo<\/code>. This approach is useful for processing data from external sources.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Expanding_Horizons_Looping_Through_Multiple_Ranges\"><\/span>Expanding Horizons: Looping Through Multiple Ranges<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Bash loops can handle more complex scenarios, such as iterating through multiple ranges. This example demonstrates the concept:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in ~\/{f..b}\/Downloads\/{1..-2}; do\n    echo $i\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home\/ajay\/f\/Downloads\/1\n\/home\/ajay\/f\/Downloads\/0\n\/home\/ajay\/f\/Downloads\/-1\n\/home\/ajay\/f\/Downloads\/-2\n\/home\/ajay\/e\/Downloads\/1\n\/home\/ajay\/e\/Downloads\/0\n\/home\/ajay\/e\/Downloads\/-1\n\/home\/ajay\/e\/Downloads\/-2\n\/home\/ajay\/d\/Downloads\/1\n\/home\/ajay\/d\/Downloads\/0\n\/home\/ajay\/d\/Downloads\/-1\n\/home\/ajay\/d\/Downloads\/-2\n\/home\/ajay\/c\/Downloads\/1\n\/home\/ajay\/c\/Downloads\/0\n\/home\/ajay\/c\/Downloads\/-1\n\/home\/ajay\/c\/Downloads\/-2\n\/home\/ajay\/b\/Downloads\/1\n\/home\/ajay\/b\/Downloads\/0\n\/home\/ajay\/b\/Downloads\/-1\n\/home\/ajay\/b\/Downloads\/-2<\/code><\/pre>\n\n\n\n<p>In this intricate loop, multiple ranges are combined to generate a variety of paths. This technique is particularly useful when you need to work with dynamic data patterns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creative_Usage_Creating_a_Timer_with_Loops\"><\/span>Creative Usage: Creating a Timer with Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Loops can be creatively employed for various tasks. Here&#8217;s an example script* of using loops to create a simple timer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# convert time into seconds\ntime=\"$(echo \"$@\" | sed 's\/h\/*3600+\/g;s\/m\/*60+\/g;s\/s\/*1+\/g;s\/+$\/\/' | bc | sed 's\/\\..*\/\/')\"\n\n# print the remaining time\nfor ((i=1;i&lt;=time;i=i+1))\ndo\n  printf '\\033&#091;0K%s\\r' \"$(((time-i)\/60)):$(((time-i)%60))\"\n  sleep 1\ndone<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>23:56<\/code><\/pre>\n\n\n\n<p>For this to work, first you need to create a script with a name, say timer.sh. <a href=\"https:\/\/smarttech101.com\/how-to-create-shell-scripts-in-linux-unix\/\" target=\"_blank\" rel=\"noreferrer noopener\">To learn about how to work with script, look over here<\/a>. <\/p>\n\n\n\n<p>This script accepts input in hours, minutes, and seconds, converts it to seconds, and then creates a countdown timer that prints the remaining time in the format <strong>&#8220;minutes:seconds&#8221;<\/strong> (ex &#8211; <code>23:56<\/code> -&gt; <code>23:55<\/code> -&gt; <code>23:54<\/code>&#8230;). <\/p>\n\n\n\n<p>You can run commands like <code>timer.sh 1h 3s<\/code> or <code>timer.sh 30m 5m 2s<\/code>.<\/p>\n\n\n\n<p>You can also append a music after the loop using <code>ffplay<\/code> and\/or <a href=\"https:\/\/smarttech101.com\/how-to-send-notifications-in-linux-using-dunstify-notify-send\/\">add a notification using <code>notify-send<\/code>,<\/a> to get notification and\/or music after the time is over.<\/p>\n\n\n\n<p>It&#8217;s a unique example of how loops can be applied to creative real-world scenarios.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Loops are the backbone of bash scripting, empowering you to automate repetitive tasks and manage complex operations. Whether you&#8217;re iterating through arrays, files, or command outputs, understanding the different types of loops and their applications is essential. By mastering loop scripting in bash, you unlock the potential to create efficient and robust automation solutions for various scenarios. <a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Looping-Constructs.html\">For official guide from GNU, look at this manual<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn the ins and outs of loop scripting in Bash. Discover various loop types, applications, and real-world examples in this comprehensive guide. From basic iterations to dynamic file processing and creative timer implementation, you&#8217;ll gain the expertise needed to efficiently automate tasks and manage complex operations using loops in Bash.<\/p>\n","protected":false},"author":2,"featured_media":2958,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[58],"tags":[18,35],"class_list":["post-2945","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","tag-command-line-tools","tag-core-linux-utilities"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Use Loops in Bash? | SmartTech101<\/title>\n<meta name=\"description\" content=\"Master the art of loops in bash scripting. Explore for, while, until, applications, and examples in this comprehensive guide.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use Loops in Bash? | SmartTech101\" \/>\n<meta property=\"og:description\" content=\"Master the art of loops in bash scripting. Explore for, while, until, applications, and examples in this comprehensive guide.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/\" \/>\n<meta property=\"og:site_name\" content=\"SmartTech101\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-05T13:20:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-17T09:39:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ajay\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ajay_yadav\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajay\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/\"},\"author\":{\"name\":\"Ajay\",\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/2edfee738a82f9c963210f8cdb438334\"},\"headline\":\"How to Use Loops in Bash?\",\"datePublished\":\"2023-08-05T13:20:53+00:00\",\"dateModified\":\"2023-08-17T09:39:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/\"},\"wordCount\":1019,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633\"},\"image\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1\",\"keywords\":[\"Command Line Tools\",\"core linux utilities\"],\"articleSection\":[\"bash\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/\",\"url\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/\",\"name\":\"How to Use Loops in Bash? | SmartTech101\",\"isPartOf\":{\"@id\":\"https:\/\/smarttech101.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1\",\"datePublished\":\"2023-08-05T13:20:53+00:00\",\"dateModified\":\"2023-08-17T09:39:59+00:00\",\"description\":\"Master the art of loops in bash scripting. Explore for, while, until, applications, and examples in this comprehensive guide.\",\"breadcrumb\":{\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1\",\"width\":1280,\"height\":720,\"caption\":\"How to Use Loops in Bash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/smarttech101.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use Loops in Bash?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/smarttech101.com\/#website\",\"url\":\"https:\/\/smarttech101.com\/\",\"name\":\"SmartTech101\",\"description\":\"Do Everything in Linux\",\"publisher\":{\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/smarttech101.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633\",\"name\":\"Ajay Yadav\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2022\/09\/cropped-ST101_logo.png?fit=180%2C60&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2022\/09\/cropped-ST101_logo.png?fit=180%2C60&ssl=1\",\"width\":180,\"height\":60,\"caption\":\"Ajay Yadav\"},\"logo\":{\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/2edfee738a82f9c963210f8cdb438334\",\"name\":\"Ajay\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/smarttech101.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6eea348caae2173954765a7cdf6cd107?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6eea348caae2173954765a7cdf6cd107?s=96&d=mm&r=g\",\"caption\":\"Ajay\"},\"sameAs\":[\"https:\/\/x.com\/ajay_yadav\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use Loops in Bash? | SmartTech101","description":"Master the art of loops in bash scripting. Explore for, while, until, applications, and examples in this comprehensive guide.","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:\/\/smarttech101.com\/how-to-use-loops-in-bash\/","og_locale":"en_US","og_type":"article","og_title":"How to Use Loops in Bash? | SmartTech101","og_description":"Master the art of loops in bash scripting. Explore for, while, until, applications, and examples in this comprehensive guide.","og_url":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/","og_site_name":"SmartTech101","article_published_time":"2023-08-05T13:20:53+00:00","article_modified_time":"2023-08-17T09:39:59+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png","type":"image\/png"}],"author":"Ajay","twitter_card":"summary_large_image","twitter_creator":"@ajay_yadav","twitter_misc":{"Written by":"Ajay","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#article","isPartOf":{"@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/"},"author":{"name":"Ajay","@id":"https:\/\/smarttech101.com\/#\/schema\/person\/2edfee738a82f9c963210f8cdb438334"},"headline":"How to Use Loops in Bash?","datePublished":"2023-08-05T13:20:53+00:00","dateModified":"2023-08-17T09:39:59+00:00","mainEntityOfPage":{"@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/"},"wordCount":1019,"commentCount":1,"publisher":{"@id":"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633"},"image":{"@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1","keywords":["Command Line Tools","core linux utilities"],"articleSection":["bash"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/","url":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/","name":"How to Use Loops in Bash? | SmartTech101","isPartOf":{"@id":"https:\/\/smarttech101.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#primaryimage"},"image":{"@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1","datePublished":"2023-08-05T13:20:53+00:00","dateModified":"2023-08-17T09:39:59+00:00","description":"Master the art of loops in bash scripting. Explore for, while, until, applications, and examples in this comprehensive guide.","breadcrumb":{"@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#primaryimage","url":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1","contentUrl":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1","width":1280,"height":720,"caption":"How to Use Loops in Bash"},{"@type":"BreadcrumbList","@id":"https:\/\/smarttech101.com\/how-to-use-loops-in-bash\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smarttech101.com\/"},{"@type":"ListItem","position":2,"name":"How to Use Loops in Bash?"}]},{"@type":"WebSite","@id":"https:\/\/smarttech101.com\/#website","url":"https:\/\/smarttech101.com\/","name":"SmartTech101","description":"Do Everything in Linux","publisher":{"@id":"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smarttech101.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smarttech101.com\/#\/schema\/person\/e8d5aebc510d698e11e9df6291381633","name":"Ajay Yadav","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smarttech101.com\/#\/schema\/person\/image\/","url":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2022\/09\/cropped-ST101_logo.png?fit=180%2C60&ssl=1","contentUrl":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2022\/09\/cropped-ST101_logo.png?fit=180%2C60&ssl=1","width":180,"height":60,"caption":"Ajay Yadav"},"logo":{"@id":"https:\/\/smarttech101.com\/#\/schema\/person\/image\/"}},{"@type":"Person","@id":"https:\/\/smarttech101.com\/#\/schema\/person\/2edfee738a82f9c963210f8cdb438334","name":"Ajay","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smarttech101.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6eea348caae2173954765a7cdf6cd107?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6eea348caae2173954765a7cdf6cd107?s=96&d=mm&r=g","caption":"Ajay"},"sameAs":["https:\/\/x.com\/ajay_yadav"]}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/smarttech101.com\/wp-content\/uploads\/2023\/08\/How-to-Use-Loops-in-Bash.png?fit=1280%2C720&ssl=1","_links":{"self":[{"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/posts\/2945"}],"collection":[{"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/comments?post=2945"}],"version-history":[{"count":5,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/posts\/2945\/revisions"}],"predecessor-version":[{"id":3034,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/posts\/2945\/revisions\/3034"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/media\/2958"}],"wp:attachment":[{"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/media?parent=2945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/categories?post=2945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smarttech101.com\/wp-json\/wp\/v2\/tags?post=2945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}