{"id":29569,"date":"2023-12-04T11:40:15","date_gmt":"2023-12-04T11:40:15","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=29569"},"modified":"2024-03-31T09:58:07","modified_gmt":"2024-03-31T09:58:07","slug":"bash-while-loop","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/","title":{"rendered":"&#8220;while&#8221; Loop in Bash"},"content":{"rendered":"<p style=\"text-align: justify;\">Bash presents different types of <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\">loops<\/a><\/u><\/strong> and one of them is the <strong>while loop<\/strong>. A while loop in bash is a set of commands that repeat as long as a certain condition is met. To automate repetitive tasks, it is crucial to learn about the while loop. In this article, I will provide an overview of the while loop with its behavior under various conditions, as well as some of the key commands associated with a while loop, such as <strong>break<\/strong>, <strong>continue<\/strong>, etc. Additionally, <strong>7 practical cases<\/strong> will be presented to illustrate the concept.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/Practice_files_bash-while-loop.zip-20231227T053026Z-001.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 &#8220;while&#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\/while-loop\/#What_is_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\" >What is &#8220;while&#8221; Loop in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#7_Cases_of_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\" >7 Cases of &#8220;while&#8221; Loop 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-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#1_%E2%80%9Cwhile%E2%80%9D_Loop_With_Single_Condition_in_Bash\" >1. &#8220;while&#8221; Loop With Single Condition in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#2_%E2%80%9Cwhile%E2%80%9D_Loop_With_Multiple_Conditions_in_Bash\" >2. &#8220;while&#8221; Loop With Multiple Conditions in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#3_Infinite_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\" >3. Infinite &#8220;while&#8221; Loop in Bash<\/a><\/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\/while-loop\/#4_%E2%80%9Cwhile%E2%80%9D_Loop_With_%E2%80%9Cbreak%E2%80%9D_Command\" >4. &#8220;while&#8221; Loop With &#8220;break&#8221; Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#5_%E2%80%9Cwhile%E2%80%9D_Loop_With_%E2%80%9Ccontinue%E2%80%9D_Command\" >5. &#8220;while&#8221; Loop With &#8220;continue&#8221; Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#6_Nested_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\" >6. Nested &#8220;while&#8221; Loop in Bash<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#7_%E2%80%9Cwhile%E2%80%9D_Loop_in_One_Line\" >7. &#8220;while&#8221; Loop in One Line<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#What_is_the_difference_between_a_while_loop_and_a_do-while_loop_in_Bash\" >What is the difference between a while loop and a do-while loop in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#How_can_I_use_a_while_loop_to_wait_for_a_condition\" >How can I use a while loop to wait for a condition?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#Can_I_nest_while_loops_in_bash\" >Can I nest while loops in bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#Is_it_possible_to_exit_a_while_loop_before_a_condition_is_false\" >Is it possible to exit a while loop before a condition is false?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#How_to_read_a_file_line_with_a_while_loop_in_bash\" >How to read a file line with a while loop in bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#What_is_C-style_while_loop_in_bash\" >What is C-style while loop 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\/while-loop\/#How_do_I_count_the_number_of_iterations_in_a_while_loop_in_bash\" >How do I count the number of iterations in a while loop in bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\"><\/span>What is &#8220;while&#8221; Loop in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In Bash, a <strong>while loop<\/strong> is a repeating sequence of statements, provided that a specified condition is true. The loop evaluates the condition before executing any commands. It is used to iterate through a series of operations that enable you to perform repetitive tasks. Once the operation is completed, the loop is terminated with the <strong>done<\/strong> keyword. Let\u2019s see an illustration for a clearer overview of how the while loop works:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-29693 size-full\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-flowchart-1.png\" alt=\"flowchart of while loop\" width=\"364\" height=\"452\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-flowchart-1.png 364w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-flowchart-1-242x300.png 242w\" sizes=\"(max-width: 364px) 100vw, 364px\" \/><\/p>\n<p style=\"text-align: justify;\">Now, let\u2019s see the basic syntax of the while loop:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">while [ condition ];\ndo\n#Statements to be executed as long as the condition is true\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<ul>\n<li style=\"text-align: justify;\"><strong>while [ condition ]:<\/strong> Starts the while loop, which will run as long as the condition is <strong>true<\/strong>. You can replace this condition to check your desired one.<\/li>\n<li style=\"text-align: justify;\"><strong>do:<\/strong> Indicates the <strong>start<\/strong> of the statements that execute in every iteration of the loop.<\/li>\n<li style=\"text-align: justify;\"><strong>#Statements to be executed as long as the condition is true:<\/strong> These statements will be executed over and over again as long as the condition is <strong>true<\/strong>.<\/li>\n<li style=\"text-align: justify;\"><strong>done:<\/strong> Marks the <strong>end<\/strong> of the while loop. <\/div><\/div><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"7_Cases_of_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\"><\/span>7 Cases of &#8220;while&#8221; Loop in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In this section, I will explain <strong>7<\/strong> different cases of the while loop. How the while loop works with single or multiple conditions will be described here. Then the infinite while loop, break and continue statements, nested while loops, etc. will be covered. Finally, I\u2019ll demonstrate the while loop in one line over multiline codes.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_%E2%80%9Cwhile%E2%80%9D_Loop_With_Single_Condition_in_Bash\"><\/span>1. &#8220;while&#8221; Loop With Single Condition in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The while loop can check a single condition and execute the output based on the condition. Here, the condition is <code class=\"\" data-line=\"\">$number -le 5<\/code> which checks whether the number is less than or equal to <strong>5<\/strong>. The <code class=\"\" data-line=\"\">((number++))<\/code> syntax increases the number in each iteration by <strong>1<\/strong> until the condition is met. Follow the script below to see the entire process:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nnumber=1\nwhile [ $number -le 5 ]; do\necho &quot;Iteration: $number&quot;\n((number++))\ndone\necho &quot;Iteration completed.&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The while loop is initiated by taking a variable<strong> number<\/strong> with the value of <strong>1<\/strong> and incrementing it by <strong>1 <\/strong>in each iteration. The output of each loop is printed by the echo &#8220;Iteration: $number&#8221;. The loop is then continued until the value is equal to or less than <strong>5<\/strong>. Upon completion of the loop, it <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\">echoes<\/a><\/u><\/strong> the message \u201cIteration completed\u201d.<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-29618\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/1.while-loop-with-single-condition.png\" alt=\"while loop with single condition\" width=\"538\" height=\"228\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/1.while-loop-with-single-condition.png 538w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/1.while-loop-with-single-condition-300x127.png 300w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/p>\n<p style=\"text-align: justify;\">In this picture, you can see that the script executes <strong>5 iterations<\/strong> and completes the loop when the value is <strong>5<\/strong> to meet the specified condition.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_%E2%80%9Cwhile%E2%80%9D_Loop_With_Multiple_Conditions_in_Bash\"><\/span>2. &#8220;while&#8221; Loop With Multiple Conditions in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To run multiple conditions using a while loop, you can use <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/logical-operators\/\" target=\"_blank\" rel=\"noopener\">logical operators<\/a><\/u><\/strong> to combine the conditions. I will use the <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/logical-operators\/or\/\" target=\"_blank\" rel=\"noopener\">OR<\/a><\/u><\/strong> <code class=\"\" data-line=\"\">||<\/code> operator in this example to execute multiple conditions within a while loop. The conditions involved two <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/comparison-operators\/\" target=\"_blank\" rel=\"noopener\">comparison operators<\/a> <\/u><\/strong>\u00a0<code class=\"\" data-line=\"\">-lt<\/code> (<strong>less than)<\/strong> and <code class=\"\" data-line=\"\">-eq<\/code> (<strong>equal to)<\/strong>. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nread -p &quot;Enter number1: &quot; number1\nread -p &quot;Enter number2: &quot; number2\nwhile [[ $number1 -lt $number2 || $number1 -eq $number2 ]];\ndo\necho $number1\n((number1++))\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 style=\"text-align: justify;\">After running the script, it prompts the users to enter <strong>number1<\/strong> and <strong>number2<\/strong>. The while loop prints the number1 and increments it by <strong>1<\/strong> in every iteration if it is equal to or less than the number2. When the condition is met, number1 becomes greater than number2, the loop terminates.<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-29619\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/2.while-loop-with-multiple-conditions.png\" alt=\"while loop with multiple conditions\" width=\"614\" height=\"277\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/2.while-loop-with-multiple-conditions.png 614w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/2.while-loop-with-multiple-conditions-300x135.png 300w\" sizes=\"(max-width: 614px) 100vw, 614px\" \/><\/p>\n<p style=\"text-align: justify;\">As you can see, after each loop, <strong>number1= 1<\/strong> increases by <strong>1<\/strong>, and the loop continues until number1 is equal to or less than <strong>number2= 3<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Infinite_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\"><\/span>3. Infinite &#8220;while&#8221; Loop in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">An infinite while loop is a kind of loop that runs infinitely. There is no end to the loop. If the conditions are always <strong>true<\/strong>, then the <strong>infinite while loop<\/strong> will be created and it will continue to run until you press <strong>CTRL+C<\/strong> in the terminal to forcefully end the loop. Let&#8217;s look at the bash script to see how the infinite loop works:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n#infinite while loop\nwhile :\ndo\necho &quot;Welcome to Linuxsimply.&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 style=\"text-align: justify;\">The infinite while loop is created as the <code class=\"\" data-line=\"\">\u201c:\u201d<\/code> built-in command always returns <strong>true<\/strong>. After starting the loop, it continues to echo &#8220;Welcome to Linuxsimply.&#8221; over and over in the terminal.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29620\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/3.while-infinite-loop.png\" alt=\"infinite while loop in bash\" width=\"593\" height=\"532\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/3.while-infinite-loop.png 593w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/3.while-infinite-loop-300x269.png 300w\" sizes=\"(max-width: 593px) 100vw, 593px\" \/><\/p>\n<p style=\"text-align: justify;\">Running the script with <code class=\"\" data-line=\"\">infinite.sh<\/code>, it shows an infinite loop that prints <strong>Welcome to Linuxsimply<\/strong> infinitely. To exit out of the infinite loop, type <strong>CTRL+C<\/strong> and press <strong>ENTER<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_%E2%80%9Cwhile%E2%80%9D_Loop_With_%E2%80%9Cbreak%E2%80%9D_Command\"><\/span>4. &#8220;while&#8221; Loop With &#8220;break&#8221; Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Break command is essential in controlling any loop. It lets you get out of a while loop without having to give any explicit command. In this example, I will show how to exit from an <strong>infinite while loop<\/strong> using the break command. Let&#8217;s look at the bash script to understand how the break command works here:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nnumber=1\nwhile :\ndo\necho $number\nif [ $number -eq 5 ]; then\necho &quot;End of the loop&quot;\nbreak\nfi\n((number++))\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 style=\"text-align: justify;\">Here, <code class=\"\" data-line=\"\">while :<\/code> will create an infinite loop but when the condition <code class=\"\" data-line=\"\">$number -eq 5<\/code> within the <code class=\"\" data-line=\"\">if<\/code> statement is satisfied, the <code class=\"\" data-line=\"\">break<\/code> statement will break the infinite loop. The <code class=\"\" data-line=\"\">-eq<\/code> operator inside the condition checks the equality between numbers. When you run the script, it creates an infinite loop and then prints the value of the current number in every loop. When the number gets to <strong>5<\/strong>, it prints \u201cEnd of the Loop\u201d and exits the loop with the break command.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29621\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/4.while-loop-with-break-command.png\" alt=\"while loop with break command\" width=\"540\" height=\"228\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/4.while-loop-with-break-command.png 540w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/4.while-loop-with-break-command-300x127.png 300w\" sizes=\"(max-width: 540px) 100vw, 540px\" \/><\/p>\n<p style=\"text-align: justify;\">As you can see, the loop is terminated when the number becomes <strong>5<\/strong>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_%E2%80%9Cwhile%E2%80%9D_Loop_With_%E2%80%9Ccontinue%E2%80%9D_Command\"><\/span>5. &#8220;while&#8221; Loop With &#8220;continue&#8221; Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The continue command is another important command to control the flow of the loop. It is the <strong>opposite<\/strong> of the break command. It jumps to the start of the loop block and iterates again by skipping certain commands based on certain conditions. Here\u2019s an example of it:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nnumber=-3\nwhile [ $number -lt 5 ]; do\n((number++))\n\n# Check if the number is negative or positive\nif [ $number -lt 0 ]; then\necho &quot;Skipping negative number: $number&quot;\ncontinue\nfi\necho &quot;Positive number: $number&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 style=\"text-align: justify;\">The script initializes by setting the number to <strong>-3<\/strong>. The while loop increments the number until it reaches <strong>5<\/strong>. During this time, the <code class=\"\" data-line=\"\">[ $number -lt 0 ]<\/code> condition with the <code class=\"\" data-line=\"\">-lt<\/code> (less than) option checks whether the current number is less than <strong>zero<\/strong> (negative number). If the number is negative, it jumps to the next iteration using the <code class=\"\" data-line=\"\">continue<\/code> command. Otherwise, it prints the positive numbers.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-32489\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/5.while-loop-with-bash-continue-command.png\" alt=\"while loop with continue command in bash\" width=\"663\" height=\"284\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/5.while-loop-with-bash-continue-command.png 663w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/5.while-loop-with-bash-continue-command-300x129.png 300w\" sizes=\"(max-width: 663px) 100vw, 663px\" \/><\/p>\n<p style=\"text-align: justify;\">In this image you can observe that, the script displays all positive numbers <strong>0 to 5<\/strong> skipping negative numbers <strong>-2<\/strong> and <strong>-1<\/strong> with the continue command.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Nested_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\"><\/span>6. Nested &#8220;while&#8221; Loop in Bash<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A nested while loop is executing one while loop inside another while loop. The structure executes a set of commands within the <strong>inner loop<\/strong> after each iteration of the <strong>outer loop<\/strong>. In this example, I will show how a nested while loop works. For that, an inner while loop condition is nested inside another outer while loop condition. Let\u2019s check the whole bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nouter_loop=1\nwhile [ $outer_loop -lt 4 ]; do\ninner_loop=1\nwhile [ $inner_loop -lt 5 ]; do\nresult=$((outer_loop + inner_loop))\necho &quot;$outer_loop + $inner_loop = $result&quot;\n((inner_loop++))\ndone\n((outer_loop++))\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 style=\"text-align: justify;\">The outer loop in this script iterates until the number is less than <strong>4<\/strong>, and the inner loop will iterate until the number is less than <strong>5<\/strong>. Then, the result of the addition of the outer loop and inner loop will be printed in the terminal. Both the outer and inner loops are incremented at the end of their respective loop to control the iteration flow.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29623\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/6.Nested-while-loop.png\" alt=\"nested while loop in bash\" width=\"412\" height=\"423\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/6.Nested-while-loop.png 412w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/6.Nested-while-loop-292x300.png 292w\" sizes=\"(max-width: 412px) 100vw, 412px\" \/><\/p>\n<p style=\"text-align: justify;\">This output shows the combinations and outcomes of the addition operations in the nested while loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"7_%E2%80%9Cwhile%E2%80%9D_Loop_in_One_Line\"><\/span>7. &#8220;while&#8221; Loop in One Line<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Multiline while loop codes can be written in one line using semicolon <strong>(;)<\/strong>. The semicolon separates the loop variable, loop condition, loop body, and all other parts. The use of a single-line while loop is advantageous, particularly while dealing with straightforward syntax. Nevertheless, for more complex operations, this syntax can be avoided to preserve the readability of the script.<\/p>\n<p style=\"text-align: justify;\">Here\u2019s an example of one line while code to increase the number from <strong>0 to 5<\/strong>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nnumber=0; while [ $number -lt 6 ]; do echo &quot;$number&quot;; ((number++)); done<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p style=\"text-align: justify;\">The loop initializes the variable number with a <strong>0<\/strong> value. Then the loop starts and continues until the number becomes less than <strong>6<\/strong>. During each iteration, it prints the current value of the number.<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29624\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/7.while-loop-in-one-line.png\" alt=\"one line while loop\" width=\"459\" height=\"256\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/7.while-loop-in-one-line.png 459w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/7.while-loop-in-one-line-300x167.png 300w\" sizes=\"(max-width: 459px) 100vw, 459px\" \/><\/p>\n<p style=\"text-align: justify;\">This image shows that the number increases by <strong>1<\/strong> in each iteration and ends with <strong>5<\/strong> since the condition is satisfied.<\/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;\">To sum up, here I&#8217;ve shown you how a while loop works and given you <strong>7 cases<\/strong> of how to use a while loop. If you&#8217;ve read this article, you&#8217;ll know about the fundamentals of while loop. By mastering these basics, you will be able to handle the while loop more efficiently. Have a great read!<\/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=\"What_is_the_difference_between_a_while_loop_and_a_do-while_loop_in_Bash\"><\/span>What is the difference between a while loop and a do-while loop in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">There is no do-while loop in bash. In contrast to other programming languages, the while loop ensures that the loop body executes at least once because the condition is evaluated after the loop is executed.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_can_I_use_a_while_loop_to_wait_for_a_condition\"><\/span>How can I use a while loop to wait for a condition?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can use the <code class=\"\" data-line=\"\">sleep<\/code> command between the while loop iterations to wait for a condition. For example:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">while [ Condition ]; do\nsleep 1\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">Here <strong>sleep 1<\/strong> introduces a delay of <strong>1 second<\/strong> in each iteration of the loop. This command is commonly used to pause the execution of a script or a loop for a specified duration.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_I_nest_while_loops_in_bash\"><\/span>Can I nest while loops in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Yes, definitely, you can nest while loops like other programming languages in bash. Here&#8217;s a syntax of nested while loop:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">while [ outer loop condition ]; do\n# Outer loop statement\nwhile [ inner loop condition ]; do\n# Inner loop statement\ndone\n# Statement after the inner loop\ndone<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Is_it_possible_to_exit_a_while_loop_before_a_condition_is_false\"><\/span>Is it possible to exit a while loop before a condition is false?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Yes, you can use the <code class=\"\" data-line=\"\">break<\/code> command to exit a while loop before a condition is false. Here\u2019s the syntax of the break command.<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">while [ condition ]; do\n# Statement to be executed\nif [ condition to break ]; then\nbreak\nfi\ndone<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"How_to_read_a_file_line_with_a_while_loop_in_bash\"><\/span>How to read a file line with a while loop in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To read a file line with a while loop in bash, you can use the <code class=\"\" data-line=\"\">read -r<\/code> command in the while loop. Here&#8217;s the full bash script for this:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n# Check if a filename is provided as a command-line argument\nif [ $# -eq 0 ]; then\n    echo &quot;Usage: $0 &lt;filename&gt;&quot;\n    exit 1\nfi\n\nfilename=$1\n# Use a while loop to read the file line by line\nwhile IFS= read -r line; do\n    echo &quot;Line: $line&quot;\n    # Add your processing logic here\ndone &lt; &quot;$filename&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\">This script takes the filename as a command line argument and then reads the file directly. If the file doesn&#8217;t exist, it will show an error.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_C-style_while_loop_in_bash\"><\/span>What is C-style while loop in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The C-style while loop in bash is the loop that follows the syntax and structure of the while loop of the C programming language. Here&#8217;s the basic syntax:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\ncounter=0\nwhile ((counter &lt; 5))\ndo\n  echo &quot;Counter: $counter&quot;\n  ((counter++))\ndone<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_count_the_number_of_iterations_in_a_while_loop_in_bash\"><\/span>How do I count the number of iterations in a while loop in bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can count the number of iterations in a while loop with <code class=\"\" data-line=\"\">((counter++))<\/code> syntax inside the while loop. It increments the iteration number in each loop by <strong>1<\/strong>. Here&#8217;s the bash script for this:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n# Initialize the counter variable\ncounter=0\nwhile [ &quot;$counter&quot; -lt 10 ]; do\n    # Increment the counter\n    ((counter++))\ndone\n# Display the number of iterations\necho &quot;Number of iterations: $counter&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\">The while loop continues to increment the iteration number from 0 to 9 and when the number becomes 10, the loop will be terminated. The output of this script will be: <strong>Number of iterations: 10.<\/strong><br \/>\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": {\n    \"@type\": \"Question\",\n    \"name\": \"What is the difference between a while loop and a do-while loop in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"There is no do-while loop in bash. In contrast to other programming languages, the while loop ensures that the loop body executes at least once because the condition is evaluated after the loop is executed.\"\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\/while-loop\/while-true-loop\/\" target=\"_blank\" rel=\"noopener\">&#8220;while true&#8221; Loop in Bash [4 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/example\/\" target=\"_blank\" rel=\"noopener\">8 Examples of \u201cwhile\u201d Loop in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/\" target=\"_blank\" rel=\"noopener\">How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/increment-number\/\" target=\"_blank\" rel=\"noopener\">How to Increment Number Using Bash \u201cwhile\u201d Loop [8 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/infinite-loop-one-line\/\" target=\"_blank\" rel=\"noopener\">One Line Infinite \u201cwhile\u201d Loop in Bash [4 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/while-loop-continue\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash Continue with \u201cwhile\u201d Loop [7 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/\" target=\"_blank\" rel=\"noopener\">Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/while-loop-one-line\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash One Line \u201cwhile\u201d Loop [8 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/sleep\/\" target=\"_blank\" rel=\"noopener\">How to Use \u201csleep\u201d Command in Bash \u201cwhile\u201d Loop [6 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;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;29569&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;10&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4.9&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;4.9\\\/5 - (10 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;\\u0026quot;while\\u0026quot; Loop in Bash&quot;,&quot;width&quot;:&quot;139.6&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 139.6px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            4.9\/5 - (10 votes)    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash presents different types of loops and one of them is the while loop. A while loop in bash is &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"&#8220;while&#8221; Loop in Bash\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#more-29569\" aria-label=\"Read more about &#8220;while&#8221; Loop in Bash\">Read more<\/a><\/p>\n","protected":false},"author":314908,"featured_media":29626,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[174],"class_list":["post-29569","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-bash-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>&quot;while&quot; Loop in Bash - LinuxSimply<\/title>\n<meta name=\"description\" content=\"While loop in bash is important to control the flow of statements, I have explained 7 practical cases of a while loop in this article.\" \/>\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\/while-loop\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"&quot;while&quot; Loop in Bash - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"While loop in bash is important to control the flow of statements, I have explained 7 practical cases of a while loop in this article.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/mitalimostakim.mou\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-04T11:40:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-31T09:58:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-in-bash.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=\"Mitu Akter Mou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mitu Akter Mou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 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\\\/while-loop\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/\"},\"author\":{\"name\":\"Mitu Akter Mou\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\"},\"headline\":\"&#8220;while&#8221; Loop in Bash\",\"datePublished\":\"2023-12-04T11:40:15+00:00\",\"dateModified\":\"2024-03-31T09:58:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/\"},\"wordCount\":1951,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/while-loop-in-bash.png\",\"keywords\":[\"bash loop\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/\",\"name\":\"\\\"while\\\" Loop in Bash - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/while-loop-in-bash.png\",\"datePublished\":\"2023-12-04T11:40:15+00:00\",\"dateModified\":\"2024-03-31T09:58:07+00:00\",\"description\":\"While loop in bash is important to control the flow of statements, I have explained 7 practical cases of a while loop in this article.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/while-loop-in-bash.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/12\\\/while-loop-in-bash.png\",\"width\":400,\"height\":400,\"caption\":\"while loop in bash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/#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\":\"&#8220;while&#8221; 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\\\/54b35370776bb6a79376ea0135412689\",\"name\":\"Mitu Akter Mou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"caption\":\"Mitu Akter Mou\"},\"description\":\"Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering &amp; Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/mitalimostakim.mou\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/mitu-akter-mou-915537198\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/mou\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\"while\" Loop in Bash - LinuxSimply","description":"While loop in bash is important to control the flow of statements, I have explained 7 practical cases of a while loop in this article.","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\/while-loop\/","og_locale":"en_US","og_type":"article","og_title":"\"while\" Loop in Bash - LinuxSimply","og_description":"While loop in bash is important to control the flow of statements, I have explained 7 practical cases of a while loop in this article.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/mitalimostakim.mou\/","article_published_time":"2023-12-04T11:40:15+00:00","article_modified_time":"2024-03-31T09:58:07+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-in-bash.png","type":"image\/png"}],"author":"Mitu Akter Mou","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mitu Akter Mou","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/"},"author":{"name":"Mitu Akter Mou","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689"},"headline":"&#8220;while&#8221; Loop in Bash","datePublished":"2023-12-04T11:40:15+00:00","dateModified":"2024-03-31T09:58:07+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/"},"wordCount":1951,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-in-bash.png","keywords":["bash loop"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/","name":"\"while\" Loop in Bash - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-in-bash.png","datePublished":"2023-12-04T11:40:15+00:00","dateModified":"2024-03-31T09:58:07+00:00","description":"While loop in bash is important to control the flow of statements, I have explained 7 practical cases of a while loop in this article.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-in-bash.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/12\/while-loop-in-bash.png","width":400,"height":400,"caption":"while loop in bash"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/#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":"&#8220;while&#8221; 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\/54b35370776bb6a79376ea0135412689","name":"Mitu Akter Mou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","caption":"Mitu Akter Mou"},"description":"Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering &amp; Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/mitalimostakim.mou\/","https:\/\/www.linkedin.com\/in\/mitu-akter-mou-915537198\/"],"url":"https:\/\/linuxsimply.com\/author\/mou\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/29569","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\/314908"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=29569"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/29569\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/29626"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=29569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=29569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=29569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}