{"id":32427,"date":"2022-12-23T06:39:09","date_gmt":"2022-12-23T06:39:09","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32427"},"modified":"2022-12-23T06:39:12","modified_gmt":"2022-12-23T06:39:12","slug":"shell-scripting-tutorials-looping-iteration-statatement-using-for-loop","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/shell-scripting-tutorials-looping-iteration-statatement-using-for-loop\/","title":{"rendered":"Shell Scripting Tutorials: Looping &amp; Iteration statatement using for loop"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"500\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-1-1.jpg\" alt=\"\" class=\"wp-image-32429\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-1-1.jpg 900w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-1-1-300x167.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-1-1-768x427.jpg 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Loops<\/strong>\u00a0are a programming construct which allow us to repeat a command or set of commands for each item in a list. As such they are key to productivity improvements through automation. Similar to wildcards and tab completion, using loops also reduces the amount of typing required (and hence reduces the number of typing mistakes).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of For Loop<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"346\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-21-1024x346.png\" alt=\"\" class=\"wp-image-32439\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-21-1024x346.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-21-300x101.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-21-768x259.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/image-21.png 1075w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example of For Loop<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"317\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-1.png\" alt=\"\" class=\"wp-image-32440\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-1.png 595w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-1-300x160.png 300w\" sizes=\"auto, (max-width: 595px) 100vw, 595px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">There are some key points of &#8216;for loop&#8217; statement:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each block of &#8216;for loop&#8217; in bash starts with &#8216;do&#8217; keyword followed by the commands inside the block. The &#8216;for loop&#8217; statement is closed by &#8216;done&#8217; keyword.<\/li>\n\n\n\n<li>The number of time for which a &#8216;for loop&#8217; will iterate depends on the declared list variables.<\/li>\n\n\n\n<li>The loop will select one item from the list and assign the value on a variable which will be used within the loop.<\/li>\n\n\n\n<li>After the execution of commands between &#8216;do&#8217; and &#8216;done&#8217;, the loop goes back to the top and select the next item from the list and repeat the whole process.<\/li>\n\n\n\n<li>The list can contain numbers or string etc. separated by spaces.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How For Loop works?<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"503\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-2-1024x503.jpg\" alt=\"\" class=\"wp-image-32441\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-2-1024x503.jpg 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-2-300x147.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-2-768x377.jpg 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-2-1536x754.jpg 1536w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/12\/bash-shell-scripting-loop-for-2.jpg 1552w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Write Simple Shell Script using For Loop<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/bin\/bash<\/span>\r\n<span class=\"hljs-keyword\">for<\/span> i in <span class=\"hljs-number\">1<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-number\">4<\/span> <span class=\"hljs-number\">5<\/span>\r\n<span class=\"hljs-keyword\">do<\/span>\r\n <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Hello $i\"<\/span>\r\ndone<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Let\u2019s inspect each element:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>#!\/bin\/bash \u2013<\/strong>&nbsp;shows that the code is a bash script<\/li>\n\n\n\n<li><strong>i<\/strong>&nbsp;\u2013 is a placeholder for a variable. Meanwhile, $i is the individual value of the variable. You can also write it as&nbsp;<strong>c\/$c<\/strong>&nbsp;or by any other name<\/li>\n\n\n\n<li><strong>in<\/strong>&nbsp;\u2013 separates the variable and the items that follow<\/li>\n\n\n\n<li><strong>1 2 3 4 5<\/strong>&nbsp;\u2013&nbsp; is an example of items you want to perform the instruction on<\/li>\n\n\n\n<li><strong>do&nbsp;<\/strong>\u2013 is the keyword that starts the loops. It will then execute the instruction n times, with n being the total number of items. Here, the value of n is 5<\/li>\n\n\n\n<li><strong>echo \u201cHello: $i\u201d<\/strong>&nbsp;\u2013 is the code which we will repeat n times. Remember quotation marks turn anything inside it into one variable.<\/li>\n\n\n\n<li><strong>done<\/strong>&nbsp;\u2013 stops the loop<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example Program<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/589a77d1bd9f6e0899eab1503b467381.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>Loops\u00a0are a programming construct which allow us to repeat a command or set of commands for each item in a list. As such they are key to productivity improvements through&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-32427","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32427"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32427\/revisions"}],"predecessor-version":[{"id":32442,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32427\/revisions\/32442"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}