title: Echo showing wrong variable value in bash after a while block. Why?
body: <p>I wanted to make a quick script to count the number of lines my .scala files have:</p>
  
  <pre><code>#!/bin/bash
  
  counter=0;
  find -iname "*.scala" | while read f; do
      lc=$(cat $f | wc -l);
      counter=$((counter+lc));
      echo "$lc $counter";
  done
  echo "final result: $counter";
  </code></pre>
  
  <p>But unfortunately this prints</p>
  
  <pre><code>20 20
  204 224
  212 436
  final result: 0
  </code></pre>
  
  <p>What's wrong here?</p>