{"id":57,"date":"2009-07-10T02:43:32","date_gmt":"2009-07-09T23:43:32","guid":{"rendered":"http:\/\/www.held.org.il\/blog\/?p=57"},"modified":"2009-07-10T02:43:32","modified_gmt":"2009-07-09T23:43:32","slug":"bash-evilness","status":"publish","type":"post","link":"http:\/\/www.held.org.il\/blog\/2009\/07\/bash-evilness\/","title":{"rendered":"bash evilness"},"content":{"rendered":"<p>I've been trying to read a file in bash, and keep all the lines in a single variable, printing it at once after the loop:<\/p>\n<p><code><br \/>\nall=\"<strong>EVIL_BASH<\/strong>\"<br \/>\ncat \/tmp\/myfile | while read line; do<br \/>\nall=\"$all $line\"<br \/>\ndone<br \/>\necho $all<br \/>\n<\/code><\/p>\n<p>However the output was only:<strong> EVIL_BASH, <\/strong>instead of having the lines of the <em>\/tmp\/myfile<\/em> appended.<\/p>\n<p><span style=\"color: #888888;\"><strong>WHAT THE HELL?<\/strong><\/span><\/p>\n<p>Because of the PIPE, <strong>the whole 'while' loop runs in another subshell<\/strong>. This means that <strong>$all<\/strong> outside the loop\u00c2\u00a0 <strong>is not the same $all from within the loop!<\/strong><\/p>\n<p>Sounds simply like scoping (as in perl), but a really awkward one. Beware.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been trying to read a file in bash, and keep all the lines in a single variable, printing it at once after the loop: all=&#8221;EVIL_BASH&#8221; cat \/tmp\/myfile | while read line; do all=&#8221;$all $line&#8221; done echo $all However the output was only: EVIL_BASH, instead of having the lines of the \/tmp\/myfile appended. WHAT THE [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75],"tags":[22,198,203,103],"_links":{"self":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/57"}],"collection":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":0,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}