{"id":218,"date":"2016-04-08T11:12:55","date_gmt":"2016-04-08T11:12:55","guid":{"rendered":"https:\/\/www.root42.de\/blog\/?p=218"},"modified":"2016-04-08T11:28:13","modified_gmt":"2016-04-08T11:28:13","slug":"how-to-concatenate-strings-efficiently-in-bash","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=218","title":{"rendered":"How to concatenate strings efficiently in bash"},"content":{"rendered":"<p>The naive implementation for concatenating strings in bash goes something like this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">#!\/bin\/bash\r\n\r\nstatement=&quot;1234567890&quot;\r\n\r\nresult=&quot;&quot;\r\n\r\nfor i in $(seq 1 100000)\r\ndo\r\n  result=${result}${statement}\r\ndone\r\n\r\nwc &lt;&lt;&lt; ${result}<\/pre>\n<p>This takes around two minutes on a modern machine. This is slow. Very, very slow.<\/p>\n<p>Instead you can build an intermediate array and use the star operator to expand it into a string. Make sure to change the input field seperator to empty, so that you don&#8217;t get spaces in between the individual entries:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">#!\/bin\/bash\r\n\r\nstatement=&quot;1234567890&quot;\r\n\r\nfor i in $(seq 1 100000)\r\ndo\r\nstatements&#x5B;${#statements&#x5B;@]}]=${statement}\r\ndone\r\n\r\nIFS= eval 'result=&quot;${statements&#x5B;*]}&quot;'\r\n\r\nwc &lt;&lt;&lt; ${result}<\/pre>\n<p>This will run in a few hundred milliseconds. You get roughly three orders of magnitude speedup.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The naive implementation for concatenating strings in bash goes something like this: #!\/bin\/bash statement=&quot;1234567890&quot; result=&quot;&quot; for i in $(seq 1 100000) do result=${result}${statement} done wc &lt;&lt;&lt; ${result} This takes around two minutes on a modern machine. This is slow. Very, very slow. Instead you can build an intermediate array and use the star operator to &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=218\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to concatenate strings efficiently in bash&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27,7,2,10],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/218"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=218"}],"version-history":[{"count":4,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/218\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/218\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}