{"id":160,"date":"2018-10-17T05:02:00","date_gmt":"2018-10-17T05:02:00","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/know-about-bash-for-loop-in-linux-with-examples\/"},"modified":"2018-10-17T05:02:00","modified_gmt":"2018-10-17T05:02:00","slug":"know-about-bash-for-loop-in-linux-with-examples","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/know-about-bash-for-loop-in-linux-with-examples\/","title":{"rendered":"Know About Bash for Loop in Linux with Examples"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" alt=\"bash for loop\" height=\"341\" src=\"https:\/\/linoxide.com\/wp-content\/uploads\/2018\/10\/bash-for-loop.png\" width=\"713\" \/><\/p>\n<p>How to make automation with bash for loop under Linux\/UNIX operating system? How do I use break, continue and the parameter for loop control expression? How to act on files using for loop?<\/p>\n<p>For loop is a very useful tool to solve many problems in the programming world and therefore we will solve some problems in the real world. In Linux we use loops via Bash, Python to make automation like password script, counting script. In this article, we will explain all of the kind of loops for Bash.<\/p>\n<h2>1) for loop<\/h2>\n<p>We can use For loop to read all elements in a list or part of them and displaying these elements on the screen.<\/p>\n<h4>Syntax of For loop<\/h4>\n<p>for variable in element1 element2 element3 Or &lt;List&gt;<br \/>\ndo<br \/>\ncommands<br \/>\ndone<\/p>\n<p>We will make a script to send greeting message for each day of the week, so we will use for loop to read each day and print it out. Create file welcome.sh with nano editor and paste code below.<\/p>\n<p> #!\/bin\/bash&#xD;<br \/>\n for day in Sunday Saturday Monday Tuesday Wednesday Thursday Friday&#xD;<br \/>\n do&#xD;<br \/>\n echo &#8220;Welcome $day day&#8221;&#xD;<br \/>\n done&#xD;<\/p>\n<p>Run the welcome.sh file with bash command.<\/p>\n<p>$ bash welcome.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\nWelcome Sunday day&#xD;<br \/>\nWelcome Saturday day&#xD;<br \/>\nWelcome Monday day&#xD;<br \/>\nWelcome Tuesday day&#xD;<br \/>\nWelcome Wednesday day&#xD;<br \/>\nWelcome Thursday day&#xD;<br \/>\nWelcome Friday day&#xD;<\/p>\n<p>The for loop will take each item in the list respectively, and assign this item to the variable $day, after that execute the code between do and done then go back to the top, assign the next item in the list and repeat over.<\/p>\n<p>We can use a range with for loop to put start point and end point.<\/p>\n<p> #!\/bin\/bash&#xD;<br \/>\n for i in &#xD;<br \/>\n do&#xD;<br \/>\n echo &#8220;Welcome $i times&#8221;&#xD;<br \/>\n done&#xD;<\/p>\n<p>$ bash range.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\nWelcome 10 times&#xD;<br \/>\nWelcome 11 times&#xD;<br \/>\nWelcome 12 times&#xD;<br \/>\nWelcome 13 times&#xD;<br \/>\nWelcome 14 times&#xD;<br \/>\nWelcome 15 times&#xD;<br \/>\nWelcome 16 times&#xD;<br \/>\nWelcome 17 times&#xD;<br \/>\nWelcome 18 times&#xD;<br \/>\nWelcome 19 times&#xD;<br \/>\nWelcome 20 times&#xD;<\/p>\n<p>If we want to count even number from 1 to 20, we will add step for range.<\/p>\n<p> #!\/bin\/bash&#xD;<br \/>\n for i in &#xD;<br \/>\n do&#xD;<br \/>\n echo &#8220;Welcome $i times&#8221;&#xD;<br \/>\n done&#xD;<\/p>\n<p>$ bash even.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\nWelcome 10 times&#xD;<br \/>\nWelcome 12 times&#xD;<br \/>\nWelcome 14 times&#xD;<br \/>\nWelcome 16 times&#xD;<br \/>\nWelcome 18 times&#xD;<br \/>\nWelcome 20 times&#xD;<\/p>\n<p>Another type of using step with for loop.<\/p>\n<p> #!\/bin\/bash&#xD;<br \/>\n for ((i=1;i&lt;=25;i+=5))&#xD;<br \/>\n do&#xD;<br \/>\n echo $i&#xD;<br \/>\n done&#xD;<\/p>\n<p>$ bash step5.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\n1&#xD;<br \/>\n6&#xD;<br \/>\n11&#xD;<br \/>\n16&#xD;<br \/>\n21&#xD;<\/p>\n<p>And for negative step we can use the following type.<\/p>\n<p> #!\/bin\/bash&#xD;<br \/>\n for ((i=25;i&gt;=1;i-=5))&#xD;<br \/>\n do&#xD;<br \/>\n echo $i&#xD;<br \/>\n done&#xD;<\/p>\n<p>$ bash step-5.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\n25&#xD;<br \/>\n20&#xD;<br \/>\n15&#xD;<br \/>\n10&#xD;<br \/>\n5&#xD;<\/p>\n<h3>Examples on For loop<\/h3>\n<h3>1) Acting on files using for loop<\/h3>\n<p>Bash For loop is the best way when we are working on files.<\/p>\n<p>#!\/bin\/bash&#xD;<br \/>\nfor file in ~\/*.txt&#xD;<br \/>\ndo&#xD;<br \/>\n echo $file&#xD;<br \/>\ndone&#xD;<\/p>\n<p>$ bash find_txt.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\n\/root\/cat.txt&#xD;<br \/>\n\/root\/echo.txt&#xD;<br \/>\n\/root\/file.txt&#xD;<br \/>\n\/root\/f.txt&#xD;<br \/>\n\/root\/nano.txt&#xD;<br \/>\n\/root\/printf.txt&#xD;<\/p>\n<h3>2) One line For loop<\/h3>\n<p>we can execute For loop in one line, we will rename all *.txt files to remove the file extension.<\/p>\n<p>$ for filename in *.txt; do mv &#8220;$filename&#8221; &#8220;$&#8221;; done<\/p>\n<p>Or in script<\/p>\n<p> !#\/bin\/bash&#xD;<br \/>\n for filename in *.txt&#xD;<br \/>\n do&#xD;<br \/>\n mv &#8220;$filename&#8221; &#8220;$&#8221;&#xD;<br \/>\n done&#xD;<\/p>\n<h3>3) Reading Command-line arguments<\/h3>\n<p>When we are executing For loop script, we can enter arguments.<\/p>\n<p> for myvalue in $*&#xD;<br \/>\n do&#xD;<br \/>\n echo &#8220;Argument: $myvalue&#8221;&#xD;<br \/>\n done&#xD;<\/p>\n<p>$ bash linoxide.sh I Love LinOxide<br \/>\noutput&#xD;<br \/>\nArgument: I&#xD;<br \/>\nArgument: Love&#xD;<br \/>\nArgument: LinOxide&#xD;<\/p>\n<h3>4) Reading odd and even number<\/h3>\n<p>We will write script to read the even and the odd numbers.<\/p>\n<p>for (( n=10; n&lt;=15; n++ ))&#xD;<br \/>\ndo&#xD;<br \/>\n if (( $n%2==0 ))&#xD;<br \/>\n then&#xD;<br \/>\n echo &#8220;$n is even&#8221;&#xD;<br \/>\n else&#xD;<br \/>\n echo &#8220;$n is odd&#8221;&#xD;<br \/>\n fi&#xD;<br \/>\ndone&#xD;<\/p>\n<p>$ bash even_odd.sh<br \/>\noutput&#xD;<br \/>\n10 is even&#xD;<br \/>\n11 is odd&#xD;<br \/>\n12 is even&#xD;<br \/>\n13 is odd&#xD;<br \/>\n14 is even&#xD;<br \/>\n15 is odd&#xD;<\/p>\n<h2>2) while loop<\/h2>\n<p>While loop depend on the condition is true, if the condition is false the interpreter get out from the loop.<\/p>\n<h4>Syntax of while loop<\/h4>\n<p>while [ &lt;some condition&gt; ]do<br \/>\n&lt;commands&gt;<br \/>\ndone<\/p>\n<p>We will count from 10 to 20 and print out the results. So we will put a condition that the counter less than or equal 20.<\/p>\n<p> #!\/bin\/bash&#xD;<br \/>\n # Basic while loop&#xD;<br \/>\n counter=10&#xD;<br \/>\n while [ $counter -le 20 ]&#xD;<br \/>\n do&#xD;<br \/>\n echo Number : $counter&#xD;<br \/>\n ((counter++))&#xD;<br \/>\n done&#xD;<\/p>\n<p>$ bash while.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\nNumber : 10&#xD;<br \/>\nNumber : 11&#xD;<br \/>\nNumber : 12&#xD;<br \/>\nNumber : 13&#xD;<br \/>\nNumber : 14&#xD;<br \/>\nNumber : 15&#xD;<br \/>\nNumber : 16&#xD;<br \/>\nNumber : 17&#xD;<br \/>\nNumber : 18&#xD;<br \/>\nNumber : 19&#xD;<br \/>\nNumber : 20&#xD;<\/p>\n<h2>3) Until loop<\/h2>\n<p>Until loop like while loop but the interpreter excute the commands within it until the condition becomes true.<\/p>\n<h4>Syntax of until loop<\/h4>\n<p>until [ &lt;some condition&gt; ]do<br \/>\n&lt;commands&gt;<br \/>\ndone<\/p>\n<p>We will count from 10 to 20 and print out the results. So we will put a condition that the counter greater than or equal 20.<\/p>\n<p> #!\/bin\/bash&#xD;<br \/>\n # Basic Until loop&#xD;<br \/>\n counter=10&#xD;<br \/>\n until [ $counter -gt 20 ]&#xD;<br \/>\n do&#xD;<br \/>\n echo Number : $counter&#xD;<br \/>\n ((counter++))&#xD;<br \/>\n done&#xD;<\/p>\n<p>$ bash until.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\nNumber : 10&#xD;<br \/>\nNumber : 11&#xD;<br \/>\nNumber : 12&#xD;<br \/>\nNumber : 13&#xD;<br \/>\nNumber : 14&#xD;<br \/>\nNumber : 15&#xD;<br \/>\nNumber : 16&#xD;<br \/>\nNumber : 17&#xD;<br \/>\nNumber : 18&#xD;<br \/>\nNumber : 19&#xD;<br \/>\nNumber : 20&#xD;<\/p>\n<h2>4) Controlling loops<\/h2>\n<p>We can use Break or Continue to control loops.<\/p>\n<h3>Break statement<\/h3>\n<p>for Break statement, we can get out from the loop and no need to complete the loop when we use if statement inside the loop.<\/p>\n<p>#!\/bin\/bash&#xD;<br \/>\n# Basic loop use break&#xD;<br \/>\ncounter=10&#xD;<br \/>\nuntil [ $counter -gt 20 ]&#xD;<br \/>\ndo&#xD;<br \/>\n echo Number : $counter&#xD;<br \/>\n if [ $counter -eq 15 ]&#xD;<br \/>\n then&#xD;<br \/>\n echo Done&#xD;<br \/>\n break&#xD;<br \/>\n fi&#xD;<br \/>\n ((counter++))&#xD;<br \/>\ndone&#xD;<\/p>\n<p>$ bash break.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\nNumber : 10&#xD;<br \/>\nNumber : 11&#xD;<br \/>\nNumber : 12&#xD;<br \/>\nNumber : 13&#xD;<br \/>\nNumber : 14&#xD;<br \/>\nNumber : 15&#xD;<br \/>\nDone&#xD;<\/p>\n<h3>Continue statement<\/h3>\n<p>For Continue statement, we can go on the loop and no need to finish the loop when we are using if statement inside the loop.<\/p>\n<p>#!\/bin\/sh&#xD;<br \/>\n&#xD;<br \/>\nNUMS=&#8221;1 2 3 4 5 6 7&#8243;&#xD;<br \/>\n&#xD;<br \/>\nfor NUM in $NUMS&#xD;<br \/>\ndo&#xD;<br \/>\n Q=`expr $NUM % 2`&#xD;<br \/>\n if [ $Q -eq 0 ]&#xD;<br \/>\n then&#xD;<br \/>\n echo &#8220;A number is an even number!!&#8221;&#xD;<br \/>\n continue&#xD;<br \/>\n fi&#xD;<br \/>\n echo &#8220;Found odd number&#8221;&#xD;<br \/>\ndone&#xD;<br \/>\n&#xD;<\/p>\n<p>$ bash continue.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\nFound odd number&#xD;<br \/>\nA number is an even number!!&#xD;<br \/>\nFound odd number&#xD;<br \/>\nA number is an even number!!&#xD;<br \/>\nFound odd number&#xD;<br \/>\nA number is an even number!!&#xD;<br \/>\nFound odd number&#xD;<\/p>\n<h2>5) Select loop<\/h2>\n<p>Select loop like while and until loop but allows you to create a simple menu system.<\/p>\n<h4>Syntax of select loop<\/h4>\n<p>select var in &lt;list&gt;<br \/>\ndo<br \/>\n&lt;commands&gt;<br \/>\ndone<\/p>\n<p>We will make three options and use select loop to choose from them.<\/p>\n<p> #!\/bin\/bash&#xD;<br \/>\n # select script&#xD;<br \/>\n&#xD;<br \/>\n PS3=&#8217;Please enter your choice: &#8216;&#xD;<br \/>\n options=(&#8220;Option 1&#8221; &#8220;Option 2&#8221; &#8220;Option 3&#8221; &#8220;Quit&#8221;)&#xD;<br \/>\n select opt in &#8220;$&#8221;&#xD;<br \/>\n do&#xD;<br \/>\n case $opt in&#xD;<br \/>\n &#8220;Option 1&#8221;)&#xD;<br \/>\n echo &#8220;you choose choice 1&#8243;&#xD;<br \/>\n ;;&#xD;<br \/>\n &#8220;Option 2&#8221;)&#xD;<br \/>\n echo &#8220;you choose choice 2&#8243;&#xD;<br \/>\n ;;&#xD;<br \/>\n &#8220;Option 3&#8221;)&#xD;<br \/>\n echo &#8220;you choose choice $REPLY which is $opt&#8221;&#xD;<br \/>\n ;;&#xD;<br \/>\n &#8220;Quit&#8221;)&#xD;<br \/>\n break&#xD;<br \/>\n ;;&#xD;<br \/>\n *) echo &#8220;invalid option $REPLY&#8221;;;&#xD;<br \/>\n esac&#xD;<br \/>\n done&#xD;<\/p>\n<p>$ bash select.sh&#xD;<\/p>\n<p>output&#xD;<br \/>\n1) Option 1&#xD;<br \/>\n2) Option 2&#xD;<br \/>\n3) Option 3&#xD;<br \/>\n4) Quit&#xD;<br \/>\nPlease enter your choice: 1&#xD;<br \/>\nyou chose choice 1&#xD;<br \/>\nPlease enter your choice: 5&#xD;<br \/>\ninvalid option 5&#xD;<br \/>\nPlease enter your choice: 4&#xD;<\/p>\n<h2>Conclusion<\/h2>\n<ol>\n<li>Bash For loop used in synchronization, making password, backup and etc&#8230;<\/li>\n<li>Do while is same as while but the interpreter executes the first code without any conditions<\/li>\n<li>Break statement is very important for getting out from the loop<\/li>\n<li>Select statement is useful when we have many options<\/li>\n<\/ol>\n<h3>Read Also:<\/h3>\n<ul>\n<li><a href=\"https:\/\/linoxide.com\/linux-how-to\/what-function-shebang-linux\/\" target=\"_blank\">What is the Function of Shebang in Linux?<\/a><\/li>\n<li><a href=\"https:\/\/linoxide.com\/linux-shell-script\/shell-scripting-interview-questions-answers\/\" target=\"_blank\">70 Shell Scripting Interview Questions &amp; Answers<\/a><\/li>\n<\/ul>\n<p> <a href=\"https:\/\/linoxide.com\/linux-how-to\/learn-about-linux-bash-loop-examples\/\" target=\"_blank\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to make automation with bash for loop under Linux\/UNIX operating system? How do I use break, continue and the parameter for loop control expression? How to act on files using for loop? For loop is a very useful tool to solve many problems in the programming world and therefore we will solve some problems &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/know-about-bash-for-loop-in-linux-with-examples\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Know About Bash for Loop in Linux with Examples&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":0,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}