{"id":597,"date":"2021-03-04T03:18:20","date_gmt":"2021-03-04T03:18:20","guid":{"rendered":"http:\/\/kevinmichaelcoy.com\/blog\/?p=597"},"modified":"2021-03-04T03:18:22","modified_gmt":"2021-03-04T03:18:22","slug":"bash-loop-through-sequence-with-a-gap-or-skipping-numbers","status":"publish","type":"post","link":"http:\/\/kevinmichaelcoy.com\/blog\/2021\/03\/04\/bash-loop-through-sequence-with-a-gap-or-skipping-numbers\/","title":{"rendered":"Bash Loop Through Sequence with a Gap or Skipping Numbers"},"content":{"rendered":"\n<p>Let&#8217;s say you want iterate over a series of sequential numbers with Bash. If so, then you could do the following:<\/p>\n\n\n\n<pre title=\"Bash For In 1 through 10 Sequence\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"> $ for i in {1..10}; do echo $i; done\n 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n 10 <\/code><\/pre>\n\n\n\n<p>Now, let&#8217;s say you want to loop over a sequence of numbers, but you want to skip over by 10 each time:<\/p>\n\n\n\n<pre title=\"Bash For In Loop Skipping Every 10\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"> $ for i in `seq 0 10 100`; do echo $i; done\n 0\n 10\n 20\n 30\n 40\n 50\n 60\n 70\n 80\n 90\n 100 <\/code><\/pre>\n\n\n\n<p>Supposedly, this syntax should work, but it is not on a Mac at least: <code>for i in {0..100..10}; do echo $i; done<\/code> (see <a href=\"https:\/\/www.lifewire.com\/bash-for-loop-examples-2200575\">source<\/a>). It does work on an Amazon Linux instance though:<\/p>\n\n\n\n<pre title=\"Bash For In Loop Skipping Every 10 Works on Amazon Linux\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"> $ for i in {0..100..10}; do echo $i; done\n 0\n 10\n 20\n 30\n 40\n 50\n 60\n 70\n 80\n 90\n 100\n $ cat \/etc\/os-release\n NAME=\"Amazon Linux AMI\"\n VERSION=\"2018.03\"\n ID=\"amzn\"\n ID_LIKE=\"rhel fedora\"\n VERSION_ID=\"2018.03\"\n PRETTY_NAME=\"Amazon Linux AMI 2018.03\"\n ANSI_COLOR=\"0;33\"\n CPE_NAME=\"cpe:\/o:amazon:linux:2018.03:ga\"\n HOME_URL=\"http:\/\/aws.amazon.com\/amazon-linux-ami\/\" <\/code><\/pre>\n\n\n\n<p>Now, let&#8217;s say you want to skip over gaps that are not as well distributed or evenly spaced. This is probably not the most elegant solution; however, it works:<\/p>\n\n\n\n<pre title=\"Bash For In Loop Skipping Numbers in Sequence\" class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"> $ for i in `seq 3 5` `seq 7 10`; do echo $i; done\n 3\n 4\n 5\n 7\n 8\n 9\n 10 <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you want iterate over a series of sequential numbers with Bash. If so, then you could do the following: Now, let&#8217;s say you want to loop over a sequence of numbers, but you want to skip over by 10 each time: Supposedly, this syntax should work, but it&#8230;<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"http:\/\/kevinmichaelcoy.com\/blog\/2021\/03\/04\/bash-loop-through-sequence-with-a-gap-or-skipping-numbers\/\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,3],"tags":[282,415,414,416],"_links":{"self":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/597"}],"collection":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/comments?post=597"}],"version-history":[{"count":2,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/597\/revisions"}],"predecessor-version":[{"id":601,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/597\/revisions\/601"}],"wp:attachment":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/media?parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/categories?post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/tags?post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}