title: Bash loop not updating output correctly
body: <p>I wrote a test script in bash, to test my server. The test progress percentage is updated and replaced by the test status, either <code>OK</code>, or <code>KO</code>.</p>
  
  <p>The following output is expected : </p>
  
  <pre><code>Test 1/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 2/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 3/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 4/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 5/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 6/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 7/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 8/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  </code></pre>
  
  <p>But for some reasons, the text does not update correctly, and I get the following output :</p>
  
  <pre><code>Test 1/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 2/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 3/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 4/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 5/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
  Test 6/8 : 10 channel(s) and 100 stream(s) : Processing...
                                                0%
                                                10.00%
                                                20.00%
                                                30.00%
                                                40.00%
                                                50.00%
                                                60.00%
                                                70.00%
                                                80.00%
                                                90.00%
                                                OK (3000/3000)
  Test 7/8 : 10 channel(s) and 100 stream(s) : Processing...
                                                0%
                                                10.00%
                                                20.00%
                                                30.00%
                                                40.00%
                                                50.00%
                                                60.00%
                                                70.00%
                                                80.00%
                                                90.00%
                                                OK (3000/3000)
  Test 8/8 : 10 channel(s) and 100 stream(s) : Processing...
                                                0%
                                                10.00%
                                                20.00%
                                                30.00%
                                                40.00%
                                                50.00%
                                                60.00%
                                                70.00%
                                                80.00%
                                                90.00%
                                                OK (3000/3000)
  </code></pre>
  
  <p>As you can see, once the graphical bug appears, it keeps happening. Also note that it happens at random moments, and sometimes, it does not appear at all.</p>
  
  <p>Here is my printing loop : </p>
  
  <pre><code>for ((i=0; i &lt; d; i++));
  do
          echo "Test $((${i} + 1))/${d} : ${channelTab[i]} channel(s) and ${streamTab[i]} stream(s) : ${saveCursor}Processing..."
          sent=0
          received=0
          errors=0
          e=0
          for ((idx = 0; idx &lt; ${channelTab[i]}; idx++));
          do
                  /u/ecmg/bin/simulator ${fileNameTab[i]} &gt; /dev/null 2&lt;&amp;1 &amp;
                  pourcentage=$(bc &lt;&lt;&lt; "scale=2;((${idx}))/${channelTab[i]} *100")
                  echo ${restoreCursor}${eraseEndOfLine}${saveCursor}${pourcentage}%
                  pids[idx]=$!
                  sleep .5
          done
          for pid in ${pids[*]}; do
                  wait $pid
                  results[e]=$(grep -a "stats" "${fileNameTab[i]}.out")
                  ((e=e+1))
          done
          for ((f=0; f &lt; e; f++));
          do
                  res=$(echo "${results[f]}" | grep -o -E '[0-9]+')
                  resArray=($res)
                  ((sent=sent + ${resArray[0]}))
                  ((received=received + ${resArray[1]}))
                  ((errors=errors + ${resArray[2]}))
          done
          if [ "$sent" != "$received" ];
          then
                  echo -e "${restoreCursor}${eraseEndOfLine}${red}KO${normal} --&gt; ${errors} errors (${received}/${sent})"
          fi
          if [ "$sent" == "$received" ];
          then
                  echo -e "${restoreCursor}${eraseEndOfLine}${green}OK${normal} (${received}/${sent})"
          fi
  done
  </code></pre>
  
  <p>Here is how <code>${restorCursor}</code>, <code>${eraseEndOfLine}</code> and <code>${saveCursor}</code> are defined : </p>
  
  <pre><code>saveCursor=$'\033[s'
  restoreCursor=$'\033[u'
  eraseEndOfLine=$'\033[K'
  </code></pre>
  
  <p>Do you have any idea why is this happening ?</p>