title: How do I append to an existing string variable inside a loop in bash?
body: <p>I have a simple bash script that downloads stock prices and appends them to a variable, which is then outputted:</p>
  <pre><code>#!/bin/bash
  
  output=&quot; &quot;
  for stock in GOOG AAPL
  do
    price=$(curl -s &quot;http://download.finance.yahoo.com/d/quotes.csv?s=$stock&amp;f=l1&quot;)
    output+=&quot;$stock: $price &quot;
  done
  
  echo &quot;$output&quot;
  </code></pre>
  <p>This script only displays <code> AAPL: 524.896</code>, the last piece of data fetched. According to <a href="http://www.whatswrongwithmyscript.com/" rel="nofollow noreferrer">whatswrongwithmyscript</a>, there isn't anything wrong with the script, and I thought I was following <a href="https://stackoverflow.com/a/2250199/2766558">this answer</a> properly. <a href="https://stackoverflow.com/a/15120052/2766558">This answer</a> discussed a similar problem (appending to a string variable inside a loop) and suggested a different method which I used like this:</p>
  <pre><code>#!/bin/bash
  
  output=&quot; &quot;
  for stock in GOOG AAPL
  do
    price=$(curl -s &quot;http://download.finance.yahoo.com/d/quotes.csv?s=$stock&amp;f=l1&quot;)
    output=&quot;$output$stock: $price &quot;
  done
  
  echo &quot;$output&quot;
  </code></pre>
  <p>The output is still the same. I'm using bash 4.2.45 on debian jessie x64.</p>
  <h3>More info</h3>
  <p>I echoed the result in a loop to debug, and from the first script, this is what I get:</p>
  <pre><code> GOOG: 1030.42
   AAPL: 524.896
   AAPL: 524.896
  </code></pre>
  <p>And the second script gives the same thing:</p>
  <pre><code> GOOG: 1030.42
   AAPL: 524.896
   AAPL: 524.896
  </code></pre>