I have a Perl script that outputs some colored text under bash. For example, this will output a red string:
perl -e 'print "\e[1;31m RED \e[m"
When I pipe it to another program, e.g. vim ... | vim - , I can see the color formating clutter-characters:
^[[1;31m RED ^[[m
and I want them to be skipped. It happens for example for grep, that you see colors in bash, but they are skipped when the output is redirected.
How to do that?
If you use Term::ANSIColor for generating the escape sequences, you can set the
ANSI_COLORS_DISABLEDenvironment variable to disable escape sequence generation).You can use the
-tfile test to find out if the output is to a tty (or detect if your script is being run interactively).Then, set the environment variable in the Perl script before any sequences are output.
Something like:
should do it.