{"id":167926,"date":"2021-02-01T16:00:39","date_gmt":"2021-02-01T13:00:39","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/for-while-and-until-cloudsavvy-it\/"},"modified":"2021-02-01T16:00:39","modified_gmt":"2021-02-01T13:00:39","slug":"for-while-and-until-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/for-while-and-until-cloudsavvy-it\/","title":{"rendered":"#for, while, and until \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a32b44f556d3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" 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: #dd3333;color:#dd3333\" 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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a32b44f556d3\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/for-while-and-until-cloudsavvy-it\/#What_Are_Bash_Loops\" >What Are Bash Loops?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/for-while-and-until-cloudsavvy-it\/#for_Based_Bash_Loops\" >for Based Bash Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/for-while-and-until-cloudsavvy-it\/#while_Based_Bash_Loops\" >while Based Bash Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/for-while-and-until-cloudsavvy-it\/#until_Based_Bash_Loops\" >until Based Bash Loops<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/for-while-and-until-cloudsavvy-it\/#Wrapping_up\" >Wrapping up<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#for, while, and until \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure id=\"attachment_9368\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-9368 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/f2dc87b07edc9a9ec5418ed232785e0d\/p\/uploads\/2021\/01\/cddc0a70.png\" alt=\"\" width=\"700\" height=\"376\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-photo\/roller-coaster-on-white-background-1176702292\" data-credittext=\"Shutterstock\/ jeep5d\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/roller-coaster-on-white-background-1176702292\">Shutterstock\/ jeep5d<\/a><\/span><\/figcaption><\/figure>\n<p>Most programming languages support various forms of looping code lines. Bash natively supports \u2018while\u2019 loops, \u2018until\u2019 loops and the most well-known, \u2018for\u2019 loops. This article introduces and discusses all three.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Are_Bash_Loops\"><\/span>What Are <i>Bash Loops<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To define this a little better, we should start with the question <i>what are loops<\/i>. Loops are a programming language construct, allowing a developer to repeat (i.e. loop) certain parts, or all, of the code inside such a loop definition. It is now easy to define <i>Bash Loops<\/i> as any loop programming language construct used in Bash!<\/p>\n<p>Bash natively supports \u2018for\u2019, \u2018until\u2019 and \u2018while\u2019 based loops. Each of these has their own advantages, but you can already get a sense of their meanings just by looking at the main word idiom. For example, \u2018until\u2019 leads one to naturally think about \u2018do something until\u2019 and this indeed what a Bash \u2018until\u2019 loops does; it loops a certain amount of (or all) code until a certain condition has been met.<\/p>\n<p>Similarly, \u2018while\u2019 loops keep running until a condition is no longer true. Finally, \u2018for\u2019 loops loop, for example, a defined number of times, similar to how we would write \u2018for 70 times, do\u2026\u2019. This helps us to logically understand the unique features that each loop provides us with and to implement more readable code.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"for_Based_Bash_Loops\"><\/span><i>for<\/i> Based Bash Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For the purposes of this article, we will have a look at the newer form of defining <i>Bash for loops<\/i>. A somewhat older, less modern, definition of <i>for<\/i> loops in Bash may, for example, look something like this: <code>for i in $(seq 1 5); do echo $i; done<\/code>. Let\u2019s compare this with a cleaner, better structured, modern looking <i>for<\/i> loop:<\/p>\n<pre>for ((i=1;i&lt;=5;i++)); do echo $i; done&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9317\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/d529635c9019eac7fb6f00d11198266a\/p\/uploads\/2021\/01\/42790f97.png\" alt=\"A Bash for based loop\" width=\"353\" height=\"119\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This simple <i>for<\/i> based Bash one-liner (a term often used in Linux\/Bash circles to conceptualize a mini-<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a> written on a single line) will print the numbers 1 to 5 in sequential order. We set a start value for the <code>i<\/code> variable (<code>$i<\/code>) by assigning the value <code>1<\/code> to the same, as the first part of our <i>for<\/i> loop definition, terminated by a <code>;<\/code> delimiter.<\/p>\n<p>Next, we specify that we only one to go up to <i>\u2018smaller, or equal, to five\u2019<\/i> by using <code>i&lt;=5<\/code>. We then indicate what should happen at the end of each round, and that is increase the variable <code>i<\/code> by one, or, in a commonly used (including in the C++ language for example) coding shorthand, this renders as <code>i++<\/code>.<\/p>\n<p>Finally we specify the start of our loop code by using <code>do<\/code>, that is, after terminating our <code>for<\/code> loop definition, alike to any other statement termination, with <code>;<\/code> before the <code>do<\/code>. We also specify the end of our loop code by using <code>done<\/code> and we <code>echo<\/code> (print) the value of our variable <i>i<\/i> in between the <code>do<\/code> and <code>done<\/code>.<\/p>\n<p>Also specifically note that the <code>do<\/code> clause itself is not terminated with <code>;<\/code>, and doing so would lead to an error. Consider the <code>do<\/code> a prefix to what needs to be done, and it makes more sense. This perhaps one reason why at times it is cleaner to put things into a multi-line script, as the <code>do<\/code> can simply be the last word on a line.<\/p>\n<p>Even when defining other types of loops, we will still maintain the <code>do<\/code> and <code>done<\/code> clauses, and we will always make sure to terminate our loop definition (i.e. before <code>do<\/code>) with <code>;<\/code>, as well as terminating the end of each individual statement within our <code>do...done<\/code> loop definition with <code>;<\/code>.<\/p>\n<p>Let\u2019s put this into a small script to see more clearly how things can work that way:<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nfor ((i=1;i&lt;=10;i+=2)); do &#13;\n  echo $i&#13;\ndone&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9318\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/59e2ec4484070332de397f2936a81be3\/p\/uploads\/2021\/01\/5188c3ae.png\" alt=\"A Bash for based loop script\" width=\"288\" height=\"236\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>After making the script executable with <code>chmod +x test.sh<\/code>, we execute the same. A few small changes were introduced to the script. Note how this time, we are increasing the variable <code>i<\/code> by two each time. This done using another coding shorthand, namely <code>i+=2<\/code> which can be read as <i>increase i by two<\/i>. One can also write <code>i=i+2<\/code> in the same place, and it works exactly the same.<\/p>\n<p>We see that we start at <i>1<\/i>, and increase by <i>2<\/i> each time we go through the loop, ending at <i>9<\/i>. The reason it ends at <i>9<\/i> is that the maximum value is <i>10<\/i>. Thus, after <i>9<\/i> the next value would be <i>11<\/i>, which is greater then <i>10<\/i> and it is thus not displayed\/looped.<\/p>\n<p>Note also how the <code>;<\/code> was removed after the echo line. This because there is no need to terminate the end of a statement if there is an end-of-line situation\/character instead. This the case here; we do not have any other command after the <code>echo $i<\/code> and the line im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely ends (spaces at the end of the line would be fine too, the principle is simply not to have another command unless that new command is prefixed (and the previous one terminated) by <code>;<\/code>).<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"while_Based_Bash_Loops\"><\/span><i>while<\/i> Based Bash Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let us next have a look at a Bash loop \u2013 using the same <code>do...done<\/code> loop definition, we can define a <i>while based Bash loop<\/i> which will run as long as a given condition is true.<\/p>\n<pre>i=1; while [ $i -le 5 ]; do echo $i; i=$[ i + 1 ]; done&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9319\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/445bbcacd20fb5956d28d6843acc6993\/p\/uploads\/2021\/01\/8d448517.png\" alt=\"A Bash while based loop\" width=\"513\" height=\"122\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In this example, we do the same as our first <i>for<\/i> based loop example. Whilst the definition looks more complex (and thus a <i>for<\/i> loop may be better suited for this particular use case), it is interesting to see how we can define a <i>while<\/i> loop in the same way.<\/p>\n<p>Here we first set our <code>i<\/code> variable, manually, in a separate command terminated by <code>;<\/code>, to <i>1<\/i>. Next we start a <i>while<\/i> loop where we set a condition, in a very similar way to defining an <code>if<\/code> statement (there is a link at the end of this article to an article about <code>if<\/code> statements if you like to learn more), where we are checking if the <code>i<\/code> variable is lower or equal (<code>-le<\/code>) then <i>5<\/i>.<\/p>\n<p>After this we have our usual <code>do...done<\/code> block in which we <code>echo<\/code> our variable, and subsequently, in a new statement, manually, increase the value of our <code>i<\/code> variable by one in a mathematical calculation as defined by the <code>$[...]<\/code> Bash calculation idioms. Let\u2019s next checkout an <i>until based Bash loop<\/i><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"until_Based_Bash_Loops\"><\/span><i>until<\/i> Based Bash Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Armed with what we learned so far, we can now more readily evaluate the following <i>until<\/i> based Bash loop:<\/p>\n<pre>i=1; until [ $i -gt 5 ]; do echo $i; i=$[ i + 1 ]; done&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9320\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/dad7a37fd224cab6ca11c4469655b2a5\/p\/uploads\/2021\/01\/0c2e4de2.png\" alt=\"A Bash until based loop\" width=\"489\" height=\"120\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Here we look for the condition <i>i<\/i> greater then <i>5<\/i> to become true. Until such time (i.e. an <i>until<\/i> based loop), we will print (using <code>echo<\/code>) our variable <i>i<\/i> and increase the same with one, alike to our previous <i>while<\/i> based example.<\/p>\n<p>We can see how the syntax for the <i>until<\/i> command is very similar to the <i>while<\/i> command. Also note that, unlike the <code>for<\/code> based command, these commands are looking for either a <i>true<\/i> condition to persist (with <i>while<\/i>), or a for a <i>true<\/i> condition to commence (with <i>until<\/i>). This also allows one to use other commands which can return a true\/false like output, like for example <code>grep -q<\/code>:<\/p>\n<pre>echo 1 &gt; a&#13;\nwhile grep -q '1' .\/a; do echo 'yes'; sleep 5; done&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9321\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/9a68908843e330aa2897302b6730df83\/p\/uploads\/2021\/01\/00bc96c1.png\" alt=\"Checking for a true status using grep -q in a while based Bash loop\" width=\"439\" height=\"137\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Here we add the number <i>1<\/i> to a file named <i>a<\/i>, and check for the existence of the number <i>1<\/i> within that file using a <code>grep -q<\/code> (a quiet grep). We keep doing so (i.e. <i>while<\/i>) until it is no longer true. Whilst it is true, we print the text <i>yes<\/i> and pause the loop for five seconds with <code>sleep 5<\/code>. Note how each command is terminated with <code>;<\/code> again.<\/p>\n<p>After about 17 seconds we interrupt our script by using CTRL+c, which is the universal method to stop a running process in Bash (besides the stronger and more effective CTRL+z which pauses a process immediately, but that is for another article!)<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Wrapping_up\"><\/span>Wrapping up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this article, we reviewed \u2018for\u2019, \u2018while\u2019 and \u2018until\u2019 loops which are natively available in Bash. If you are interested in more Bash, have a look at Conditional Testing in Bash: if, then, else, elif and Bash Functions and Local Variables.<\/p>\n<p><strong>Enjoy!<\/strong>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9316\/primer-bash-loops-for-while-and-until\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#for, while, and until \u2013 CloudSavvy IT&#8221; Shutterstock\/ jeep5d Most programming languages support various forms of looping code lines. Bash natively supports \u2018while\u2019 loops, \u2018until\u2019 loops and the most well-known, \u2018for\u2019 loops. This article introduces and discusses all three. What Are Bash Loops? To define this a little better, we should start with the question&#8230;<\/p>\n","protected":false},"author":1,"featured_media":167927,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/cddc0a70.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-167926","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/167926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=167926"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/167926\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/167927"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=167926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=167926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=167926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}