I was actually able to figure this out, and I figure I'd add it here for the next googler who bangs their head against the same wall.
I had a grep alias and
GREP_OPTIONS set. This caused color highlighting to remain on in the script, even when piping to another command. That usually doesn't play nicely with
Here's my .alias and options:
alias grep='grep -i --color'
So when running from the script, it doesn't use the aliased command and so forces color to always be on. So when I checked my alias and saw the
--color option (which means
auto, which means "don't color output that gets piped to another command" (like
I was confused because I forgot I had set
GREP_OPTIONS as well, so I expected the
grep in the script to have
color set to
auto by default (as it would if I hadn't set the global
GREP_OPTIONS). But not so.
Here are my new settings (I believe the
--color flag to
GREP_OPTIONS is redundant, but I leave it there as a reminder):
alias grep='grep --color=always'
export GREP_OPTIONS="--ignore-case --color"
That way, any time I am on the command line, I'll have highlighting on for all my greps (which is usually what I want). But in scripts it will default to coloring only when not piped to another command. I'll still have to add --color=always to many of my scripts (since I tend to prefer highlighting in most cases, even when piping to another command, unless I don't ever see the output).