{"id":3846,"date":"2011-03-17T07:38:00","date_gmt":"2011-03-17T07:38:00","guid":{"rendered":""},"modified":"2026-04-18T22:51:54","modified_gmt":"2026-04-18T22:51:54","slug":"bash-loop-a-comma-separated-list","status":"publish","type":"post","link":"https:\/\/hellem.org\/wblog\/?p=3846","title":{"rendered":"Bash: Loop a comma separated list"},"content":{"rendered":"<p>Just perfect!<\/p>\n<p><!-- codeblock lang=\"\" line=\"1\" --><\/p>\n<pre><code>\r\n######VARIABLE DEFINITIONS#######\r\nLIST=1,2,3,4,5\r\n#################################\r\n\r\n# Add a trailing comma to the list variable\r\nLOOPVAR=${LIST},\r\n\r\n# Loop as long as there is a comma in the variable\r\nwhile echo $LOOPVAR | grep \\, &amp;amp;&gt; \/dev\/null\r\ndo\r\n\r\n# Grab one item out of the list\r\nLOOPTEMP=${LOOPVAR%%\\,*}\r\n\r\n# Remove the item we just grabbed from the list,\r\n# as well as the trailing comma\r\nLOOPVAR=${LOOPVAR#*\\,}\r\n\r\n# some action with your variable\r\n#\r\n# echo $LOOPTEMP\r\n#\r\n# for example\r\n\r\ndone\r\n<\/code><\/pre>\n<p><!-- \/codeblock --><\/p>\n<p>Thanks to &#8220;<a href=\"http:\/\/codeandfury.blogspot.com\/2006\/12\/bash-hacks-comma-delimited-list.html\">The Code and the Fury<\/a>&#8221; for providing the script, see the blog post for more details about how the script actually works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just perfect! ######VARIABLE DEFINITIONS####### LIST=1,2,3,4,5 ################################# # Add a trailing comma to the list variable LOOPVAR=${LIST}, # Loop as long as there is a comma in the variable while echo $LOOPVAR | grep \\, &amp;amp;&gt; \/dev\/null do # Grab one item out of the list LOOPTEMP=${LOOPVAR%%\\,*} # Remove the item we just grabbed from the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-3846","post","type-post","status-publish","format-standard","hentry","category-work-geek"],"_links":{"self":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3846"}],"version-history":[{"count":1,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3846\/revisions"}],"predecessor-version":[{"id":13758,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/3846\/revisions\/13758"}],"wp:attachment":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}