{"id":1884,"date":"2015-11-08T08:05:52","date_gmt":"2015-11-08T16:05:52","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1884"},"modified":"2015-11-08T08:05:52","modified_gmt":"2015-11-08T16:05:52","slug":"for-loops-in-bash","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1884","title":{"rendered":"for loops in bash"},"content":{"rendered":"<p>I don\u2019t do much shell scripting, but every once in a while I use it to automate things. I was playing around with bash today and thought this might interest others.<\/p>\n<p>Define an array variable<br \/>\n$ servers=(server purple yellow dan);<br \/>\n$ echo $servers<br \/>\nserver<\/p>\n<p>Redefine it.<br \/>\n$ servers=(firstserver server purple yellow dan);<br \/>\n$ echo $servers<br \/>\nfirstserver<\/p>\n<p>That\u2019s not what I want. So how do we access the values? Let\u2019s try a for loop.<br \/>\n$ for name in $servers; do echo $name; done;<br \/>\n$ echo $servers<br \/>\nfirstserver<\/p>\n<p>Well that didn\u2019t do it. Let\u2019s try to access the second value with array notation.<br \/>\n$ for name in $servers; do echo $servers[1]; done;<br \/>\nfirstserver[1]<\/p>\n<p>If you look carefully at this you\u2019ll notice that what is happening is that you are concatenating the return value from $server with [1]. Try it again with this line.<\/p>\n<p>$ for name in $servers; do echo $servers[a]; done;<br \/>\nfirstserver[a]<\/p>\n<p>And again.<br \/>\n$ for name in $servers; do echo $servers.a; done;<br \/>\nfirstserver.a<\/p>\n<p>As long as you use a character that is not a valid part of a variable name, you get concatenation.<br \/>\ne.g. don\u2019t use an alphanumeric.<br \/>\n$ for name in $servers; do echo $serversabc; done;<\/p>\n<p>There is no variable called serverabc, so you get a blank line.<\/p>\n<p>It turns out that bash has weird syntax for working with variables.<br \/>\n$ echo ${servers[2]}<br \/>\npurple<\/p>\n<p>Add another value.<br \/>\n$ servers[5]=finalvalue<br \/>\n$ echo ${servers[5]}<br \/>\nfinalvalue<\/p>\n<p>So how about we separate out the loop variable like this?<br \/>\n$ for name in $servers; do echo $servers${name}; done;<br \/>\nfirstserverfirstserver<\/p>\n<p>Well, we\u2019re getting warmer.<br \/>\n$ for name in $servers; do echo $name; done;<br \/>\nfirstserver<\/p>\n<p>This is getting frustrating. Let\u2019s back up and try just listing a bunch of stuff in a list.<br \/>\n$ for name in firstserver server purple yellow dan finalvalue; do echo $name; done;<br \/>\nfirstserver<br \/>\nserver<br \/>\npurple<br \/>\nyellow<br \/>\ndan<br \/>\nfinalvalue<\/p>\n<p>So we can make a for loop iterate through items, but the normal way of accessing elements of an array doesn\u2019t work in bash. It turns out that you need special syntax.<\/p>\n<p>$ echo ${servers[@]}<br \/>\nfirstserver server purple yellow finalvalue finalvalue<\/p>\n<p>And to loop through everything like we wanted to do at the start.<br \/>\n$ for name in ${servers[@]}; do echo $name; done;<br \/>\nfirstserver<br \/>\nserver<br \/>\npurple<br \/>\nyellow<br \/>\ndan<br \/>\nfinal value<\/p>\n<p>That wasn\u2019t so hard was it?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don\u2019t do much shell scripting, but every once in a while I use it to automate things. I was playing around with bash today and thought this might interest others. Define an array variable $ servers=(server purple yellow dan); $ echo $servers server Redefine it. $ servers=(firstserver server purple yellow dan); $ echo $servers &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1884\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">for loops in bash<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1884","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1884"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1884\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}