There are times when I really question my choice to do things in shellscript. Did you know that in BSD /bin/sh and in Bash, variables inside "for" loops are global like normal, but variables inside "while" loops are automatically local to the loop? I didn't, and I wasted about an hour fruitlessly debugging everything because of it.
Specifically, "while" loops on the receiving end of a pipe. Variables are local because the loop is executed in a subshell. Oops.