WARNING: Be careful when using ((expression)).
count=0 things="0 1 0 0 1" for i in $things; do if [ $i == "1" ]; then (( count++ )) fi done echo "Count is ${count}"
returns:
2
echo $?
returns:
0
NOTE: A 0 indicates success.
A 1 indicates failure.
NOTE: This looks fine; but there is a small gotcha:
The ((expression)) is evaluated according to the Arithmetic Evaluation rules.
But if you run this script with -e or enable errexit:
bash -e test.sh
then count++ is going to return 0 (post-increment) and the script will stop.
Checking the result:
echo $?
returns:
1
This time a failure.
A definite trap to watch out for! Do not use ((expression)) here.