{"id":233,"date":"2012-04-14T23:30:20","date_gmt":"2012-04-14T23:30:20","guid":{"rendered":"https:\/\/pim.famnit.upr.si\/wp\/?p=233"},"modified":"2021-11-17T11:20:35","modified_gmt":"2021-11-17T11:20:35","slug":"bash-looping-or-capturing-a-multiple-line-command-output","status":"publish","type":"post","link":"https:\/\/pim.famnit.upr.si\/wp\/?p=233","title":{"rendered":"Bash &#8211; looping or capturing a multiple line command output"},"content":{"rendered":"<p>I tried to loop a command which returns a multi-line result in a bash script. The problem was that the quoted command returned ONE line instead of each line separately. So I ended with one line consisted of multi-line result while I was expecting that the quotes would preserve the spacing. Without quotes I got each &quot;word&quot; in a new line while I was expecting everything in one line (without quotes should replace multiple <span class=\"comment-copy\">blanks, tabs and newlines with a single space<\/span>). Here is the &quot;faulty&quot; script:<\/p>\n<pre style=\"background-color: #ffffd2;\">#for line in `ls -l`\r\n#for line in $(ls -l)\r\nfor line in \"$(ls -l)\" \r\ndo\r\n&nbsp;&nbsp;&nbsp; echo $line\"XXX\"&nbsp;&nbsp;&nbsp; \r\ndone<\/pre>\n<p>Not quite sure if this is due to OS X (or BSD) environment but I got around it by piping the command to the while loop:<\/p>\n<pre style=\"background-color: #ffffd2;\">(ls -l) | while read line\r\ndo\r\n&nbsp;&nbsp;&nbsp;&nbsp; echo $line\"xxx\"\r\ndone<\/pre>\n<p>or <\/p>\n<pre style=\"background-color: #ffffd2;\">while read line; do\r\n&nbsp;&nbsp;&nbsp; echo $line\"xxx\"\r\ndone &lt; &lt;(ls -l)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I tried to loop a command which returns a multi-line result in a bash script. The problem was that the quoted command returned ONE line instead of each line separately. So I ended with one line consisted of multi-line result while I was expecting that the quotes would preserve the spacing. Without quotes I got&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/posts\/233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=233"}],"version-history":[{"count":1,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":746,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions\/746"}],"wp:attachment":[{"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}