{"id":821,"date":"2020-08-24T23:22:46","date_gmt":"2020-08-24T17:52:46","guid":{"rendered":"https:\/\/techieshouts.com\/?p=821"},"modified":"2022-08-09T19:04:52","modified_gmt":"2022-08-09T13:34:52","slug":"for-loop-in-shell-script-over-string-list","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/for-loop-in-shell-script-over-string-list\/","title":{"rendered":"For loop in shell script"},"content":{"rendered":"\n<p>Looping through the list is a very useful and much-needed function for every scripting and programming language. The most common looping method is the programming world is the for loop. In this blog, we will see how to iterate over strings separated by a delimiter in a<a href=\"https:\/\/en.wikipedia.org\/wiki\/Shell_script\" target=\"_blank\" rel=\"noopener\"> shell script.<\/a><\/p>\n\n\n\n<h2>For loop Syntax<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for i in ${&lt;Listwithdelimiter>\/\/&lt;Delimiter>\/ }; do\n    # processing logic\n    # processing logic\ndone<\/pre>\n\n\n\n<p>Let us see an example that implements the above syntax. In this example, we will iterate over a string that has values separated by a comma.<\/p>\n\n\n\n<h3>ForIterator.sh<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sourcestr=\"Id,name,age,gender,city\"\n\necho \"The source string is - $sourcestr\"\ncnt=0\nfor i in ${sourcestr\/\/,\/ }; do\n   let \"cnt+=1\"\n   echo \"String $cnt\t: $i\"\ndone<\/pre>\n\n\n\n<p>The result of the above script upon execution is,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sh ForIterator.sh\nThe source string is - Id,name,age,gender,city\nString 1\t: Id\nString 2\t: name\nString 3\t: age\nString 4\t: gender\nString 5\t: city<\/pre>\n\n\n\n<p>Also, check &#8220;<a href=\"https:\/\/techieshouts.com\/reading-a-delimited-file-in-shell-script\/\">How to read a delimited file in shell<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looping through the list is a very useful and much-needed function for every scripting and programming language. The most common looping method is the programming world is the for loop. In this blog, we will see how to iterate over strings separated by a delimiter in a shell script. For loop Syntax Let us see\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/for-loop-in-shell-script-over-string-list\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,8],"tags":[103,104],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/821"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=821"}],"version-history":[{"count":4,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/821\/revisions"}],"predecessor-version":[{"id":835,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/821\/revisions\/835"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}