> output2.txtcd # some directory i'm trying to searchfind views/shared -type f -name *.js -print0 | while IFS= read -r -d $'\0' line; do    echo -n ${line%.js} | tee -a ~/Documents/counter/output2.txt    grep -lr ${line%.js} . | wc -l | tee -a ~/Documents/counter/output2.txt   # produce a count of occurrences    regex='[a-zA-Z]+.extend'    grep -f $line $regex    grep -lr ${line%.js} . | tee -a ~/Documents/counter/output2.txt           # produce a list of occurrencesdoneReturnsgrep: brackets ([ ]) not balancedAll the examples I've seen on the web seem to indicate there is nothing wrong here, so i'm pretty confusedSurely the square brackets are balanced, aren't they?
What's wrong with this grep?
bash;grep;osx;regular expression
Your issue is the -f option. Instead of specifying the file to search, -f specifies a file to read a list of patterns from. OS X grep's man page explains it, though not very clearly: -f file, --file=file         Read one or more newline separated patterns from file.  Empty pattern lines match every input         line.  Newlines are not considered part of a pattern.  If file is empty, nothing is matched.The help for GNU grep is actually more straightforward:$ grep --help | grep -- '-f,'  -f, --file=FILE           obtain PATTERN from FILE$ This behavior of -f is, according to GNU grep's man page, specified by POSIX.Your fix is probably to change your line:grep -f $line $regexto:egrep $regex -- $lineYou are using an extended regular expression so use egrep or grep -EThe -- will prevent grep from parsing any options in the $line variable, e.g. it would protect you against a file named -r funnyname.js