Incrementing a variable inside a Bash loop
- Always use
-r
with read. - There is no need to use
cut
, you can stick with pure bash solutions.- In this case passing
read
a 2nd var (_
) to catch the additional "fields"
- In this case passing
- Prefer
[[ ]]
over[ ]
. - Use arithmetic expressions.
- Do not forget to quote variables! Link includes other pitfalls as well
while read -r country _; do
if [[ $country = 'US' ]]; then
((USCOUNTER++))
echo "US counter $USCOUNTER"
fi
done < "$FILE"
You are using USCOUNTER
in a subshell, that's why the variable is not showing in the main shell.
Instead of cat FILE | while ...
, do just a while ... done < $FILE
. This way, you avoid the common problem of I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?:
while read country _; do
if [ "US" = "$country" ]; then
USCOUNTER=$(expr $USCOUNTER + 1)
echo "US counter $USCOUNTER"
fi
done < "$FILE"
Note I also replaced the `` expression with a $().
I also replaced while read line; do country=$(echo "$line" | cut -d' ' -f1)
with while read country _
. This allows you to say while read var1 var2 ... varN
where var1
contains the first word in the line, $var2
and so on, until $varN
containing the remaining content.