{"id":35791,"date":"2024-02-01T10:32:55","date_gmt":"2024-02-01T10:32:55","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=35791"},"modified":"2024-03-31T10:03:28","modified_gmt":"2024-03-31T10:03:28","slug":"bash-break-while-loop","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/","title":{"rendered":"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]"},"content":{"rendered":"<p style=\"text-align: justify;\">When you are working with <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\">bash loops<\/a><\/u><\/strong> and wish to exit from the loop before finishing it, you can do that using the break statement. The break statement is one of the most powerful tools that allows you to easily break a loop based on a specific condition. The conditions can be changed according to your requirements when you need to break. In this post, I will show you 11 practical examples of using the break statement within a while loop that will help you elevate your scripting skills.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/practice_files_bash-break-while-loop.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files to Exit \u201cwhile\u201d Loop in Bash Using \u201cbreak\u201d Statement<\/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\/break-while-loop\/#Basics_of_%E2%80%9Cbreak%E2%80%9D_Statement\" >Basics of \u201cbreak\u201d Statement<\/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\/break-while-loop\/#11_Examples_to_Exit_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash_Using_%E2%80%9Cbreak%E2%80%9D_Statement\" >11 Examples to Exit \u201cwhile\u201d Loop in Bash Using \u201cbreak\u201d Statement<\/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\/break-while-loop\/#1_Break_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_a_Single_Condition\" >1. Break a \u201cwhile\u201d Loop Based on a Single Condition<\/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\/break-while-loop\/#2_Based_on_Multiple_Conditions_Break_a_%E2%80%9Cwhile%E2%80%9D_Loop\" >2. Based on Multiple Conditions Break a \u201cwhile\u201d Loop<\/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\/break-while-loop\/#3_Exit_from_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_Counter\" >3. Exit from a \u201cwhile\u201d Loop Based on Counter<\/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\/break-while-loop\/#4_Break_an_Infinite_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_User_Input\" >4. Break an Infinite \u201cwhile\u201d Loop Based on User Input<\/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\/break-while-loop\/#5_Exit_out_of_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_Command\" >5. Exit out of a \u201cwhile\u201d Loop Based on 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\/break-while-loop\/#6_Break_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_Error\" >6. Break a \u201cwhile\u201d Loop Based on Error<\/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\/break-while-loop\/#7_Based_on_a_Correct_Password_Exit_from_a_%E2%80%9Cwhile%E2%80%9D_Loop\" >7. Based on a Correct Password Exit from a \u201cwhile\u201d Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#8_Break_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_Signal\" >8. Break a \u201cwhile\u201d Loop Based on Signal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#9_Exit_Out_of_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_File_Existence\" >9. Exit Out of a \u201cwhile\u201d Loop Based on File Existence<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#10_Break_%E2%80%9Cwhile%E2%80%9D_Loop_With_Function_Calls\" >10. Break \u201cwhile\u201d Loop With Function Calls<\/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\/break-while-loop\/#11_Exit_from_Nested_%E2%80%9Cwhile%E2%80%9D_Loops\" >11. Exit from Nested \u201cwhile\u201d Loops<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#Tips_for_Using_%E2%80%9Cbreak%E2%80%9D_Statement_in_Bash_%E2%80%9Cwhile%E2%80%9D_Loop\" >Tips for Using \u201cbreak\u201d Statement in Bash \u201cwhile\u201d Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#Practice_Tasks_of_Using_%E2%80%9Cbreak%E2%80%9D_Statement_in_Bash_%E2%80%9Cwhile%E2%80%9D_Loop\" >Practice Tasks of Using \u201cbreak\u201d Statement in Bash \u201cwhile\u201d Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-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-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-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-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#How_to_break_from_loop_in_shell_script\" >How to break from loop in shell script?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#What_is_while_loop_in_shell_script\" >What is while loop in shell script?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#How_do_you_break_an_infinite_while_loop\" >How do you break an infinite while loop?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#How_do_you_break_while_true\" >How do you break while true?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#What_is_the_difference_between_break_and_continue\" >What is the difference between break and continue?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#What_is_exit_criteria_for_while_loop\" >What is exit criteria for while loop?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Basics_of_%E2%80%9Cbreak%E2%80%9D_Statement\"><\/span>Basics of \u201cbreak\u201d Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">break<\/code> statement is a control structure used in various programming languages including Bash to break a loop. It allows the user to terminate a loop prematurely or switch statements based on a certain condition. Here\u2019s the flow diagram of the <strong>break<\/strong> statement:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-35808\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/break-statement-flowchart.png\" alt=\"break statement flowchart\" width=\"444\" height=\"496\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/break-statement-flowchart.png 444w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/break-statement-flowchart-269x300.png 269w\" sizes=\"(max-width: 444px) 100vw, 444px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"11_Examples_to_Exit_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash_Using_%E2%80%9Cbreak%E2%80%9D_Statement\"><\/span>11 Examples to Exit \u201cwhile\u201d Loop in Bash Using \u201cbreak\u201d Statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In a <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/\" target=\"_blank\" rel=\"noopener\">while loop<\/a><\/u><\/strong>, the break statement is used to break the loop. A loop can be interrupted based on one, or several conditions. The break statement can also be used to handle errors. In this section, I will look at <strong>11 examples<\/strong> of using a break statement to interrupt the while loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Break_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_a_Single_Condition\"><\/span>1. Break a \u201cwhile\u201d Loop Based on a Single Condition<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A <strong>break<\/strong> statement can break the while loop based on a single condition. The condition for terminating the loop in this example is <code class=\"\" data-line=\"\">[[ $n -eq 4 ]]<\/code>. In this case, when the value of <strong>n<\/strong> becomes 4, the loop will terminate. Moreover, this script uses the <code class=\"\" data-line=\"\">-le<\/code> (less than and equal to) and <code class=\"\" data-line=\"\">-eq<\/code> (equal to) operators within a while loop which is the common <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/comparison-operators\/\" target=\"_blank\" rel=\"noopener\">comparison operator<\/a><\/u><\/strong> in Bash.<\/p>\n<p style=\"text-align: justify;\">Now, follow the script to see how to <code class=\"\" data-line=\"\">break<\/code> a while loop based on a single condition:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nn=0\nwhile [[ $n -le 8 ]]; do\n\u00a0 echo $n\n\u00a0 if [[ $n -eq 4 ]]; then\n\u00a0\u00a0\u00a0 break\n\u00a0 fi\n\u00a0 ((n++))\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The script initializes with a variable <strong>n<\/strong> with a value of <strong>0<\/strong>. Inside the while loop, the condition <code class=\"\" data-line=\"\">$n -eq 4<\/code> checks whether <strong>n<\/strong> is equal to 4. If <strong>n<\/strong> is equal to 4, the <code class=\"\" data-line=\"\">break<\/code> statement terminates the while loop. Otherwise, it runs as long as the value of <strong>n<\/strong> becomes less than and equal to 8 <code class=\"\" data-line=\"\">($n -le 8)<\/code>. The value of n is incremented by 1 in each iteration with <code class=\"\" data-line=\"\">((n++))<\/code>.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-35796\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1.break-while-loop-based-on-a-single-condition.png\" alt=\"break while loop based on a single condition\" width=\"562\" height=\"227\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1.break-while-loop-based-on-a-single-condition.png 562w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/1.break-while-loop-based-on-a-single-condition-300x121.png 300w\" sizes=\"(max-width: 562px) 100vw, 562px\" \/><\/p>\n<p style=\"text-align: justify;\">As you can see the while loop breaks when the number equals 4.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Based_on_Multiple_Conditions_Break_a_%E2%80%9Cwhile%E2%80%9D_Loop\"><\/span>2. Based on Multiple Conditions Break a \u201cwhile\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The while loop can be terminated based on multiple conditions. For that, the conditions are set in the <code class=\"\" data-line=\"\">if<\/code> statement with the help of the <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/operator\/logical-operators\/and\/\" target=\"_blank\" rel=\"noopener\">AND operator<\/a><\/u><\/strong> <code class=\"\" data-line=\"\">($$)<\/code> inside the loop. Now, check out the bash script to break a <code class=\"\" data-line=\"\">while<\/code> loop based on multiple conditions using the <code class=\"\" data-line=\"\">break<\/code> statement:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nn=0\nwhile [[ $n -le 10 ]]; do\n\u00a0 if [[ $n -gt 5 &amp;&amp; $n -lt 8 ]]; then\n\u00a0\u00a0\u00a0 break\n\u00a0 fi\n\u00a0 echo $n\n\u00a0 ((n++))\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>After assigning the value 0 to the variable <strong>n<\/strong>, <code class=\"\" data-line=\"\">while [[ $n -le 10 ]]; do<\/code> starts the loop to iterate as long as the <strong>n<\/strong> is less than and equal to 10. But for the <code class=\"\" data-line=\"\">break<\/code> statement, it breaks the loop when the value of the <strong>n<\/strong> is greater than 5 <code class=\"\" data-line=\"\">($n -gt 5)<\/code> and less than 8 <code class=\"\" data-line=\"\">($n -lt 8)<\/code>. The <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a><\/u><\/strong> prints the current value of the <strong>n<\/strong> variable. <code class=\"\" data-line=\"\">((n++))<\/code> increases the value of the <strong>n<\/strong> by 1 in every iteration.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-35797\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2.break-while-loop-based-on-a-multiple-condition.png\" alt=\"break while loop based on a multiple conditions\" width=\"523\" height=\"256\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2.break-while-loop-based-on-a-multiple-condition.png 523w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/2.break-while-loop-based-on-a-multiple-condition-300x147.png 300w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/p>\n<p style=\"text-align: justify;\">Upon executing the script, it prints the numbers from 0 to 5 and the loop terminates when the number gets greater than 5 and less than 8.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Exit_from_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_Counter\"><\/span>3. Exit from a \u201cwhile\u201d Loop Based on Counter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Another way to end a while loop is to use the <code class=\"\" data-line=\"\">counter<\/code>. In bash, the counter is a variable that is incremented by 1 in every iteration using the <code class=\"\" data-line=\"\">((counter++))<\/code> syntax. When the <strong>counter<\/strong> reaches a certain value, it terminates the loop. Here\u2019s the bash script to complete the task:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\ncounter=0\nwhile [[ $counter -le 10 ]]; do\necho &quot;Counter: $counter&quot;\n\u00a0\u00a0\u00a0 ((counter++))\n\u00a0\u00a0\u00a0 if [ $counter -eq 6 ]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 fi\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>This script initializes a variable <strong>counter<\/strong> to 0 and then enters a while loop. Inside the loop, <code class=\"\" data-line=\"\">echo &quot;Counter: $counter&quot;<\/code> prints the current value of the <strong>counter<\/strong> variable. Then, it increments the value of the counter by 1 using <code class=\"\" data-line=\"\">((counter++))<\/code>.<\/p>\n<p>After that, it checks whether the counter is equal to 6 using the condition <code class=\"\" data-line=\"\">if [ $counter -eq 6 ]<\/code>. If this condition is <strong>true<\/strong>, it executes a <code class=\"\" data-line=\"\">break<\/code> statement, which will exit the loop prematurely. If the condition is <strong>false<\/strong>, the loop will continue as long as the number is less than and equal to 10.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35798\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3.break-while-loop-based-on-counter.png\" alt=\"break while loop in bash based on counter\" width=\"581\" height=\"259\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3.break-while-loop-based-on-counter.png 581w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/3.break-while-loop-based-on-counter-300x134.png 300w\" sizes=\"(max-width: 581px) 100vw, 581px\" \/><\/p>\n<p style=\"text-align: justify;\">The picture shows that the loop terminates when the counter becomes 6.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Break_an_Infinite_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_User_Input\"><\/span>4. Break an Infinite \u201cwhile\u201d Loop Based on User Input<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 forever. However, a user can provide a specific input to break the <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/infinite-loop\/#7_Infinite_while_Loop_in_Bash\" target=\"_blank\" rel=\"noopener\">infinite while loop<\/a><\/u><\/strong>. In this example, if the user type \u201cy\u201d in the question prompt, the infinite while loop will stop executing &#8220;Linuxsimply.com&#8221;. Follow the script below to understand how to break the infinite while loop based on <code class=\"\" data-line=\"\">user input<\/code>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nwhile true; do\n\u00a0\u00a0\u00a0 echo &quot;Linuxsimply.com&quot;\n\u00a0\u00a0\u00a0 read -p &quot;Do you want to break? (y\/n): &quot; user_input\n\u00a0\u00a0\u00a0 if [ &quot;$user_input&quot; == &quot;y&quot; ]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 fi\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>This script creates an infinite loop using <code class=\"\" data-line=\"\">while true<\/code>. Inside the loop, it prints &#8220;Linuxsimply.com&#8221; and prompts the user to input either \u201cy\u201d or \u201cn\u201d to determine whether to break out of the loop. If the user enters \u201cy\u201d, the script executes a <code class=\"\" data-line=\"\">break<\/code> statement, ending the loop. Otherwise, it continues the loop printing &#8220;Linuxsimply.com&#8221;.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35799\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4.break-while-loop-based-on-user-input.png\" alt=\"break while loop based on user input in bash\" width=\"700\" height=\"185\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4.break-while-loop-based-on-user-input.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/4.break-while-loop-based-on-user-input-300x79.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">The loop stops executing \u201cLinuxsimply.com\u201d when a user types \u201cy\u201d into the prompt.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Exit_out_of_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_Command\"><\/span>5. Exit out of a \u201cwhile\u201d Loop Based on Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">It is possible to exit out of a while loop based on a command. In the following example, the <strong><u><a href=\"https:\/\/linuxsimply.com\/ping-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">ping command<\/a><\/u><\/strong> is used to check the reachability of the server &#8220;linuxsimply.com&#8221;. Once the command is executed successfully, the loop will come to an end. To do this, copy the bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nserver=&quot;linuxsimply.com&quot;\nwhile true; do\n\u00a0\u00a0\u00a0 if ping -c 1 &quot;$server&quot; &amp;&gt; \/dev\/null; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Ping to $server successful! Exiting the loop.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 else\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Ping to $server unsuccessful. Retrying...&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sleep 2\n\u00a0\u00a0\u00a0 fi\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><code class=\"\" data-line=\"\">server=&quot;linuxsimply.com&quot;<\/code> sets the variable <strong>server<\/strong> to the value &#8220;linuxsimply.com&#8221;. <code class=\"\" data-line=\"\">while true; do<\/code> initiates an infinite loop. Then <code class=\"\" data-line=\"\">if ping -c 1 &quot;$server&quot; &amp;&gt; \/dev\/null<\/code> uses the <code class=\"\" data-line=\"\">ping<\/code> command to send one ICMP echo request to the specified server <strong>linuxsimply.com<\/strong>. The <code class=\"\" data-line=\"\">&amp;&gt; \/dev\/null<\/code> redirects both standard output and standard error to <code class=\"\" data-line=\"\">\/dev\/null<\/code> to suppress the output. If the <code class=\"\" data-line=\"\">ping<\/code> is successful, the script prints a success message and exits the loop using the <code class=\"\" data-line=\"\">break<\/code> command.<\/p>\n<p>If the ping is unsuccessful, the script prints an error message, waits for 2 seconds using the <code class=\"\" data-line=\"\">sleep 2<\/code> command, and then retries the ping in the next iteration of the loop.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35800\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5.break-while-loop-based-on-command.png\" alt=\"break while loop based on command\" width=\"825\" height=\"93\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5.break-while-loop-based-on-command.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5.break-while-loop-based-on-command-300x34.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/5.break-while-loop-based-on-command-768x87.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p>The loop breaks as the ping command is successful.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Break_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_Error\"><\/span>6. Break a \u201cwhile\u201d Loop Based on Error<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To handle errors, a break statement can be integrated with the <code class=\"\" data-line=\"\">$?<\/code> special variable that checks whether a particular command is successful or not. If the <code class=\"\" data-line=\"\">$?<\/code> variable returns 0, meaning that the command is executed successfully. When the variable returns more than 0, it indicates the failure of the command.<\/p>\n<p style=\"text-align: justify;\">The following bash script will break a while loop when it encounters an <code class=\"\" data-line=\"\">error<\/code>. Check out the script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nwhile true; do\n\u00a0\u00a0\u00a0 df -h\n\u00a0\u00a0\u00a0 if [ $? -ne 0 ]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Error occurred. Exiting the loop.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 else\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Command executed successfully. Continuing...&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sleep 2\n\u00a0\u00a0\u00a0 fi\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><code class=\"\" data-line=\"\">while true; do<\/code> initializes an infinite loop. Then <code class=\"\" data-line=\"\">df -h<\/code> command displays disk space usage. <code class=\"\" data-line=\"\">if [ $? -ne 0 ]<\/code> checks the exit status of the last command <code class=\"\" data-line=\"\">(df -h)<\/code>. If the exit status is not equal to 0, the script prints an error message and exits the loop using the <code class=\"\" data-line=\"\">break<\/code> statement. If the exit status is 0, the script prints a success message, and the loop continues. The <code class=\"\" data-line=\"\">sleep 2<\/code> command introduces a pause of 2 seconds before going to the next iteration of the loop.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\">After running the script, you will get the following output displaying the usage of disk space:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35801\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6.break-while-loop-based-on-error.png\" alt=\"break while loop based on error\" width=\"825\" height=\"348\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6.break-while-loop-based-on-error.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6.break-while-loop-based-on-error-300x127.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/6.break-while-loop-based-on-error-768x324.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">As no error is encountered, the loop continues. You can type <strong>CTRL+C<\/strong> to exit out of the loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"7_Based_on_a_Correct_Password_Exit_from_a_%E2%80%9Cwhile%E2%80%9D_Loop\"><\/span>7. Based on a Correct Password Exit from a \u201cwhile\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In this example, the while loop will terminate based on a correct password. The loop breaks if the password does not match the actual password of the user. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\ncorrect_password=&quot;linux&quot;\nwhile true; do\n\u00a0\u00a0\u00a0 read -p &quot;Enter the password: &quot; user_password\n\u00a0\u00a0\u00a0 if [ &quot;$user_password&quot; == &quot;$correct_password&quot; ]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Correct password! Access granted.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 else\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Incorrect password. Try again.&quot;\n\u00a0\u00a0\u00a0 fi\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><code class=\"\" data-line=\"\">correct_password=&quot;linux&quot;<\/code> sets the correct password. Then the infinite while loop starts and prompts the user to enter a password with the <code class=\"\" data-line=\"\">read -p<\/code> command. After that <code class=\"\" data-line=\"\">if [ &quot;$user_password&quot; == &quot;$correct_password&quot; ]<\/code> checks whether the entered password matches the correct password (&#8220;linux&#8221;). If the user enters the correct password, it prints a success message and exits the loop. If the entered password is incorrect, it prints an error message and prompts the user to try again.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35802\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7.break-while-loop-based-on-correct-password.png\" alt=\"break bash while loop based on correct password\" width=\"700\" height=\"183\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7.break-while-loop-based-on-correct-password.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/7.break-while-loop-based-on-correct-password-300x78.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">\u201cli\u201d is an incorrect password so it shows an error message. When the correct password \u201clinux\u201d is entered, the loop breaks with a success message.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"8_Break_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_Signal\"><\/span>8. Break a \u201cwhile\u201d Loop Based on Signal<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">trap<\/code> command is used here to catch the <strong>SIGINT<\/strong> signal usually produced by pressing <strong>Ctrl+C<\/strong>. After receiving the signal, it executes the <code class=\"\" data-line=\"\">break<\/code> command to end the loop. To know more, check the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\ntrap &#039;break&#039; SIGINT\nn=1\nwhile [ $n -le 10 ]; do\n\u00a0\u00a0\u00a0 echo &quot;Iteration number:$n&quot;\n\u00a0\u00a0 sleep 1\n\u00a0((n++))\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><code class=\"\" data-line=\"\">trap &#039;break&#039; SIGINT<\/code> sets up a trap to capture the <strong>SIGINT<\/strong> signal (generated by pressing <strong>Ctrl+C<\/strong>) and executes the <code class=\"\" data-line=\"\">break<\/code> command when the signal is received. <code class=\"\" data-line=\"\">n=1<\/code> initializes a variable <strong>n<\/strong> with the value 1. <code class=\"\" data-line=\"\">while [ $n -le 10 ]; do<\/code> starts a while loop that continues as long as the value of <strong>n<\/strong> is less than or equal to 10. <code class=\"\" data-line=\"\">echo &quot;Iteration number: $n&quot;<\/code> prints a message indicating the current iteration number. <code class=\"\" data-line=\"\">sleep 1<\/code> pauses the script for 1 second, simulating some work within each iteration. <code class=\"\" data-line=\"\">((n++))<\/code> increments the counter <strong>n<\/strong> for the next iteration.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35803\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8.break-while-loop-based-on-signal.png\" alt=\"break while loop based on signal\" width=\"579\" height=\"226\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8.break-while-loop-based-on-signal.png 579w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/8.break-while-loop-based-on-signal-300x117.png 300w\" sizes=\"(max-width: 579px) 100vw, 579px\" \/><\/p>\n<p style=\"text-align: justify;\">When I pressed <strong>CTRL+C, <\/strong>the loop ended due to the signal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"9_Exit_Out_of_a_%E2%80%9Cwhile%E2%80%9D_Loop_Based_on_File_Existence\"><\/span>9. Exit Out of a \u201cwhile\u201d Loop Based on File Existence<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To exit out of a while loop based on file existence, the <code class=\"\" data-line=\"\">-e<\/code> option is used inside the <code class=\"\" data-line=\"\">if<\/code> condition that checks whether the specified file exists. Follow the script below to learn the complete process:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nwhile true; do\n\u00a0\u00a0\u00a0 read -p &quot;Enter the filename: &quot; filename\n\u00a0\u00a0\u00a0 if [ -e &quot;$filename&quot; ]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;File &#039;$filename&#039; exists. Exiting the loop.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 fi\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>After starting the infinite while loop, it prompts the user to enter a <strong>filename<\/strong> and stores the input in the variable \u201cfilename\u201d with the <code class=\"\" data-line=\"\">read -p<\/code> command. Then <code class=\"\" data-line=\"\">if [ -e &quot;$filename&quot; ]<\/code> checks if the entered file exists, and repeats the process until an existing \u201cfilename\u201d is provided. Once the existing <strong>filename<\/strong> is entered, the script prints a success message and exits the loop using the <code class=\"\" data-line=\"\">break<\/code> command.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35804\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/9.break-while-loop-based-on-file-existence.png\" alt=\"break bash while loop based on file existence\" width=\"825\" height=\"153\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/9.break-while-loop-based-on-file-existence.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/9.break-while-loop-based-on-file-existence-300x56.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/9.break-while-loop-based-on-file-existence-768x142.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Since there is no &#8220;mou.txt&#8221; file, the loop continues. The loop ends when it discovers that the &#8220;hello.txt&#8221; file is present.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"10_Break_%E2%80%9Cwhile%E2%80%9D_Loop_With_Function_Calls\"><\/span>10. Break \u201cwhile\u201d Loop With Function Calls<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">break<\/code> statement can be executed with function calls. In this example, a function named <code class=\"\" data-line=\"\">positive_number<\/code> is created first to check whether the input number is positive or negative, then the while loop will call the function and end the loop if the given number is positive. Check out the script to know the full process:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\npositive_number() {\n\u00a0\u00a0\u00a0 local number=&quot;$1&quot;\n\u00a0\u00a0\u00a0 if [ &quot;$number&quot; -gt 0 ]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Valid positive number. Exiting the loop.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 0\n\u00a0\u00a0\u00a0 else\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Invalid number. Please enter a positive number.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 1\n\u00a0\u00a0\u00a0 fi\n}\n\nwhile true; do\n\u00a0\u00a0\u00a0 read -p &quot;Enter a positive number: &quot; n\n\u00a0\u00a0\u00a0 if positive_number &quot;$n&quot;; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 fi\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><code class=\"\" data-line=\"\">positive_number<\/code> function is defined to check whether a given number is greater than 0 (positive number) or not using the <code class=\"\" data-line=\"\">&quot;$number&quot; -gt 0<\/code> condition. Inside the while loop, the <code class=\"\" data-line=\"\">read<\/code> command with the <code class=\"\" data-line=\"\">-p<\/code> option is used to prompt the user to enter a number. The entered number is then passed to the <code class=\"\" data-line=\"\">positive_number<\/code> function.<\/p>\n<p>If the function returns 0 (indicating a valid positive number), the loop is exited using the <code class=\"\" data-line=\"\">break<\/code> statement and prints &#8220;Valid positive number. Exiting the loop.&#8221; by the <code class=\"\" data-line=\"\">echo<\/code> command. When the function returns 1 (indicating an invalid negative number), the loop continues and prints &#8220;Invalid number. Please enter a positive number.&#8221; until the user enters a positive number.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35805\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/10.break-while-loop-with-function-calls.png\" alt=\"break while loop with function calls\" width=\"700\" height=\"266\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/10.break-while-loop-with-function-calls.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/10.break-while-loop-with-function-calls-300x114.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">The loop did not break when I entered negative numbers -1 and -2. When the positive number 1 is entered, the loop terminates.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"11_Exit_from_Nested_%E2%80%9Cwhile%E2%80%9D_Loops\"><\/span>11. Exit from Nested \u201cwhile\u201d Loops<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Nested while loops contain one while loop within another while loop. When dealing with nested loops, the <code class=\"\" data-line=\"\">break<\/code> statement in Bash can take an optional numerical argument to specify which loop to exit. Here, the <code class=\"\" data-line=\"\">break 2<\/code> statement is used to exit out of both loops. Follow the bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\ni=0\nwhile [[ $i -lt 10 ]]; do\n\u00a0\u00a0\u00a0 j=0\n\u00a0\u00a0\u00a0 while [[ $j -lt 5 ]]; do\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;$i, $j&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if [[ $j -eq 3 ]]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Breaking out of both loops.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break 2\u00a0 # Breaks out of both loops\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fi\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ((j++))\n\u00a0\u00a0\u00a0 done\n\u00a0\u00a0\u00a0 ((i++))\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The outer loop runs while the variable <strong>i<\/strong> is less than 10 <code class=\"\" data-line=\"\">($i -lt 10)<\/code>. Inside the outer loop, the inner loop runs as long as the variable<strong> j<\/strong> is less than 5 <code class=\"\" data-line=\"\">($j -lt 5)<\/code>.<\/p>\n<p>Within the inner loop, the script prints the values of <strong>i<\/strong> and <strong>j<\/strong> using the <code class=\"\" data-line=\"\">echo &quot;$i, $j&quot;<\/code>. It then checks if <strong>j<\/strong> is equal to 3. If true, it prints a message and uses <code class=\"\" data-line=\"\">break 2<\/code> to exit both the inner and outer loops. If <strong>j<\/strong> is not equal to 3, the inner loop continues until <strong>j<\/strong> is no longer less than 5. <code class=\"\" data-line=\"\">((j++))<\/code> increments the value of<strong> j<\/strong> by 1 in each iteration.<\/p>\n<p>After the inner loop completes, the outer loop increments the variable <strong>i <\/strong>using <code class=\"\" data-line=\"\">((i++))<\/code>, and the process repeats until <strong>i<\/strong> is no longer less than 10.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35806\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/11.break-nested-while-loops.png\" alt=\"break nested while loops using break 2 statement\" width=\"612\" height=\"224\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/11.break-nested-while-loops.png 612w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/11.break-nested-while-loops-300x110.png 300w\" sizes=\"(max-width: 612px) 100vw, 612px\" \/><\/p>\n<p style=\"text-align: justify;\">The script breaks out of both loops when<strong> j<\/strong> is equal to 3. If you remove the \u201cbreak 2\u201d statement, the script will continue until the outer loop completes even after <strong>j<\/strong> is equal to 3 in the inner loop.<\/p>\n<p style=\"text-align: justify;\">To exit from an <strong>inner loop<\/strong> only, use <code class=\"\" data-line=\"\">break 1<\/code> statement like this:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\ni=0\nwhile [[ $i -lt 3 ]]; do\n\u00a0\u00a0\u00a0 j=0\n\u00a0\u00a0\u00a0 while [[ $j -lt 3 ]]; do\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;$i, $j&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if [[ $j -eq 1 ]]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Breaking out of the inner loop.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break 1\u00a0 # Breaks out of the inner loop\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fi\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ((j++))\n\u00a0\u00a0\u00a0 done\n\u00a0\u00a0\u00a0 ((i++))\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><code class=\"\" data-line=\"\">i=0<\/code> initializes a variable <strong>i<\/strong> to 0. <code class=\"\" data-line=\"\">while [[ $i -lt 3 ]]; do<\/code> starts an outer loop that continues as long as the value of <strong>i<\/strong> is less than 3. <code class=\"\" data-line=\"\">j=0<\/code> initializes a variable<strong> j<\/strong> to 0 within the outer loop. <code class=\"\" data-line=\"\">while [[ $j -lt 3 ]]; do<\/code> starts an inner loop that continues as long as the value of<strong> j<\/strong> is less than 3. <code class=\"\" data-line=\"\">echo &quot;$i, $j&quot;<\/code> prints the values of i and j.<\/p>\n<p><code class=\"\" data-line=\"\">$j -eq 1<\/code> within <code class=\"\" data-line=\"\">if<\/code> condition checks if the value of <strong>j <\/strong>is equal to 1. If true, it prints &#8220;Breaking out of the inner loop.&#8221; using the <code class=\"\" data-line=\"\">break 1<\/code> statement. <code class=\"\" data-line=\"\">((j++))<\/code> increments the value of j by 1. <code class=\"\" data-line=\"\">((i++))<\/code> increments the value of i by 1 for the next iteration of the outer loop.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35807\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/12.break-inner-while-loop.png\" alt=\"break inner while loop in bash using break 1 statement\" width=\"700\" height=\"343\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/12.break-inner-while-loop.png 700w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/12.break-inner-while-loop-300x147.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p style=\"text-align: justify;\">When<strong> j<\/strong> becomes 1, the script breaks out of the <strong>inner loop<\/strong> using \u201cbreak 1\u201d and allows the <strong>outer loop<\/strong> to continue executing.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Tips_for_Using_%E2%80%9Cbreak%E2%80%9D_Statement_in_Bash_%E2%80%9Cwhile%E2%80%9D_Loop\"><\/span>Tips for Using \u201cbreak\u201d Statement in Bash \u201cwhile\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Use the <code class=\"\" data-line=\"\">break<\/code> statement cautiously to exit out of the while loop to avoid unexpected results. Here are some tips on how to use the break command within the while loop:<\/p>\n<ol>\n<li style=\"text-align: justify;\"><strong> Use break command within the if conditional statement: <\/strong>It is great to use the <code class=\"\" data-line=\"\">break<\/code> command within the if statement to break upon a specific condition when you want to avoid premature break.<\/li>\n<li style=\"text-align: justify;\"><strong> Try to use labels for nested loops: <\/strong>As there are multiple loops, consider using labels to break a particular loop.<\/li>\n<li style=\"text-align: justify;\"><strong> Always test code before running: <\/strong>Test your code before running using testing tools to check the errors.<\/li>\n<li style=\"text-align: justify;\"><strong> Be mindful of exit condition: <\/strong>If you do not provide an appropriate exit condition to end your loop, you may end up with an unwanted outcome.<\/li>\n<li style=\"text-align: justify;\"><strong> Use descriptive variable names: <\/strong>The names of the variables should be descriptive, which avoids ambiguities and simplifies the debugging process.<\/li>\n<li style=\"text-align: justify;\"><strong> Control signal utilizing \u201ctap\u201d command: <\/strong>Use the <code class=\"\" data-line=\"\">tap<\/code> command that can break a loop after receiving a signal generated by pressing <strong>CTRL+C<\/strong>.<\/li>\n<li style=\"text-align: justify;\"><strong> Explain code using comments: <\/strong>Comments make the code easier to read and understandable. This is especially useful when you are dealing with complicated scripts.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Practice_Tasks_of_Using_%E2%80%9Cbreak%E2%80%9D_Statement_in_Bash_%E2%80%9Cwhile%E2%80%9D_Loop\"><\/span>Practice Tasks of Using \u201cbreak\u201d Statement in Bash \u201cwhile\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Practice the following to improve your understanding of how to use a break statement inside the Bash while loop to terminate it:<\/p>\n<ol>\n<li style=\"text-align: justify;\">Create a while loop that continues as long as the value of the variable reaches 15 and breaks a while loop when the value becomes 10.<\/li>\n<li style=\"text-align: justify;\">Write a script that uses a while loop to generate a sequence of even numbers up to 10. Break out of the loop when the loop variable becomes greater than or equal to 6.<\/li>\n<li style=\"text-align: justify;\">Make an infinite while loop that breaks when a user types \u201cNo\u201d in the prompt.<\/li>\n<li style=\"text-align: justify;\">Generate nested while loops and break the inner loop when the variable value is greater than 4.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In conclusion, I have covered <strong>11<\/strong> examples of using the \u201cbreak\u201d statement inside a while loop. Additionally, you will learn how to use the break command effectively to prevent unwanted errors. Lastly, don\u2019t forget to do the practice tasks. Good luck!<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"How_to_break_from_loop_in_shell_script\"><\/span>How to break from loop in shell script?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To break from loop in n shell scripting, you can use the <code class=\"\" data-line=\"\">break<\/code> statement. The <strong>break<\/strong> statement is typically used within loops to terminate the loop based on a certain condition. Here is an example bash script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\ncount=1\nwhile true; do\n\u00a0\u00a0\u00a0 echo &quot;Iteration $count&quot;\n\u00a0\u00a0\u00a0 if [ $count -eq 5 ]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Breaking out of the loop.&quot;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 fi\n\u00a0\u00a0\u00a0 ((count++))\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">This script breaks the loop when the value of count equals 5.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_while_loop_in_shell_script\"><\/span>What is while loop in shell script?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In a shell script, a while loop is a control structure that repeatedly executes a set of statements as long as a specified condition is true.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_you_break_an_infinite_while_loop\"><\/span>How do you break an infinite while loop?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To break an infinite while loop that runs indefinitely, use the <code class=\"\" data-line=\"\">break<\/code> command inside the infinite while loop. Here\u2019s a basic syntax for this:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nwhile true; do\n\u00a0\u00a0\u00a0 # Commands to be executed in each iteration of the loop\n\u00a0\u00a0\u00a0 if [ condition ]; then\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\n\u00a0\u00a0\u00a0 fi\ndone<\/code><\/pre>\n<p style=\"text-align: justify;\">Replace the condition according to your preference when you wish to break the loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_you_break_while_true\"><\/span>How do you break while true?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">while true<\/code> creates an infinite loop. To break it, use the <code class=\"\" data-line=\"\">break<\/code> statement within the loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_break_and_continue\"><\/span>What is the difference between break and continue?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The main difference between the break and continue statements is that the <code class=\"\" data-line=\"\">break<\/code> command breaks the loop based on a specific condition whereas the <code class=\"\" data-line=\"\">continue<\/code> command skips the rest of the commands for the current iteration and moves on to the next iteration.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_exit_criteria_for_while_loop\"><\/span>What is exit criteria for while loop?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The exit criteria for the while loop is the <strong>condition<\/strong> specified in the <code class=\"\" data-line=\"\">while<\/code> statement. The loop continues to execute as long as this condition remains <strong>true<\/strong>. Once the condition evaluates to <strong>false<\/strong>, the loop is exited. If you want to exit before finishing the loop, you can use the <code class=\"\" data-line=\"\">break<\/code> command within the loop.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"What is while loop in shell script?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"In a shell script, a while loop is a control structure that repeatedly executes a set of statements as long as a specified condition is true.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How do you break while true?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The while true creates an infinite loop. To break it, use the break statement within the loop.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is the difference between break and continue?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The main difference between the break and continue statements is that the break command breaks the loop based on a specific condition whereas the continue command skips the rest of the commands for the current iteration and moves on to the next iteration.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What is exit criteria for while loop?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"The exit criteria for the while loop is the condition specified in the while statement. The loop continues to execute as long as this condition remains true. Once the condition evaluates to false, the loop is exited. If you want to exit before finishing the loop, you can use the break command within the loop.\"\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\/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;\u201cwhile\u201d Loop in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1077955,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:{&quot;1&quot;:2,&quot;2&quot;:16777215},&quot;9&quot;:1,&quot;10&quot;:1,&quot;12&quot;:0,&quot;15&quot;:&quot;Arial&quot;,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/\" target=\"_blank\" rel=\"noopener\">\u201cwhile\u201d Loop in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Loops in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\">Loops in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span><\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;35791&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>When you are working with bash loops and wish to exit from the loop before finishing it, you can do &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#more-35791\" aria-label=\"Read more about Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]\">Read more<\/a><\/p>\n","protected":false},"author":314908,"featured_media":35809,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[176],"class_list":["post-35791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-while-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>Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"In this article, I have explained 11 examples of using the &quot;break&quot; statement inside the while loop in Bash to break the loop.\" \/>\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\/break-while-loop\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"In this article, I have explained 11 examples of using the &quot;break&quot; statement inside the while loop in Bash to break the loop.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-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=\"2024-02-01T10:32:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-31T10:03:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/Exit-while-Loop-Using-break-Command.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=\"16 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\\\/break-while-loop\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/\"},\"author\":{\"name\":\"Mitu Akter Mou\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\"},\"headline\":\"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]\",\"datePublished\":\"2024-02-01T10:32:55+00:00\",\"dateModified\":\"2024-03-31T10:03:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/\"},\"wordCount\":2968,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/Exit-while-Loop-Using-break-Command.png\",\"keywords\":[\"while loop\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/\",\"name\":\"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/Exit-while-Loop-Using-break-Command.png\",\"datePublished\":\"2024-02-01T10:32:55+00:00\",\"dateModified\":\"2024-03-31T10:03:28+00:00\",\"description\":\"In this article, I have explained 11 examples of using the \\\"break\\\" statement inside the while loop in Bash to break the loop.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-while-loop\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/Exit-while-Loop-Using-break-Command.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/Exit-while-Loop-Using-break-Command.png\",\"width\":400,\"height\":400,\"caption\":\"Exit \u201cwhile\u201d loop using \u201cbreak\u201d statement\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/break-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\":\"\\\"while\\\" Loop in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]\"}]},{\"@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":"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples] - LinuxSimply","description":"In this article, I have explained 11 examples of using the \"break\" statement inside the while loop in Bash to break the loop.","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\/break-while-loop\/","og_locale":"en_US","og_type":"article","og_title":"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples] - LinuxSimply","og_description":"In this article, I have explained 11 examples of using the \"break\" statement inside the while loop in Bash to break the loop.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/mitalimostakim.mou\/","article_published_time":"2024-02-01T10:32:55+00:00","article_modified_time":"2024-03-31T10:03:28+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/Exit-while-Loop-Using-break-Command.png","type":"image\/png"}],"author":"Mitu Akter Mou","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mitu Akter Mou","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/"},"author":{"name":"Mitu Akter Mou","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689"},"headline":"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]","datePublished":"2024-02-01T10:32:55+00:00","dateModified":"2024-03-31T10:03:28+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/"},"wordCount":2968,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/Exit-while-Loop-Using-break-Command.png","keywords":["while loop"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/","name":"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/Exit-while-Loop-Using-break-Command.png","datePublished":"2024-02-01T10:32:55+00:00","dateModified":"2024-03-31T10:03:28+00:00","description":"In this article, I have explained 11 examples of using the \"break\" statement inside the while loop in Bash to break the loop.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/Exit-while-Loop-Using-break-Command.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/02\/Exit-while-Loop-Using-break-Command.png","width":400,"height":400,"caption":"Exit \u201cwhile\u201d loop using \u201cbreak\u201d statement"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-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":"\"while\" Loop in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/"},{"@type":"ListItem","position":5,"name":"Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]"}]},{"@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\/35791","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=35791"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/35791\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/35809"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=35791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=35791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=35791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}