Bash for loop variable is global. Fix that.