title: Incrementing a variable inside a Bash loop
body: <p>I'm trying to write a small script that will count entries in a log file, and I'm incrementing  a variable (<code>USCOUNTER</code>) which I'm trying to use after the loop is done. </p>
  
  <p>But at that moment <code>USCOUNTER</code> looks to be 0 instead of the actual value. Any idea what I'm doing wrong? Thanks!</p>
  
  <pre><code>FILE=$1
  
  tail -n10 mylog &gt; $FILE
  
  USCOUNTER=0
  
  cat $FILE | while read line; do
    country=$(echo "$line" | cut -d' ' -f1)
    if [ "US" = "$country" ]; then
          USCOUNTER=`expr $USCOUNTER + 1`
          echo "US counter $USCOUNTER"
    fi
  done
  echo "final $USCOUNTER"
  </code></pre>
  
  <p>It outputs:</p>
  
  <pre><code>US counter 1
  US counter 2
  US counter 3
  ..
  final 0
  </code></pre>