Well, your best approach is to fix pygmentize
to properly return an error code. As Ignacio Vazquez-Abrams mentions, one of the distros has a patch that is either causing or fixing this.
But, here is how to work around it:
If the error message is on stderr
The easiest way is probably to redirect stderr to a temporary file, and leave stdout alone:
pygmentize "$var" 2> "$tmpfile"
then you can grep "$tmpfile"
. There are other ways, but they're more complicated.
If the error message is on stdout
Yep, that'd be another bug in pygmentize, it should be on stderr. The temporary file will work again, however. Just cat
the temporary file back to stdout if its OK. Alternatively, you can use tee
to duplicate the stdout to several destinations.