{"id":1611,"date":"2013-04-22T08:34:09","date_gmt":"2013-04-22T08:34:09","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=1611"},"modified":"2013-04-22T08:34:09","modified_gmt":"2013-04-22T08:34:09","slug":"count-variables-and-store-in-an-array-in-bash","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=1611","title":{"rendered":"Count variables and store in an Array in bash"},"content":{"rendered":"<p>The $# expansion will tell you the number of elements in a variable \/ array. If you&#8217;re working with a bash version greater than 2.05 or so you can:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">VAR='some string with words'<br \/>\nVAR=( $VAR )<br \/>\necho ${#VAR[@]}<\/div><\/div>\n<p>This effectively splits the string into an array along whitespace (which is the default delimiter), and then counts the members of the array.<\/p>\n<p>EDIT:<\/p>\n<p>Of course, this recasts the variable as an array. If you don&#8217;t want that, use a different variable name or recast the variable back into a string:<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">VAR=&quot;${VAR[*]}&quot;<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The $# expansion will tell you the number of elements in a variable \/ array. If you&#8217;re working with a bash version [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1611","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1611"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1611\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}