Where do my ANSI escape codes go when I pipe to another process? Can I keep them?

8,975

Solution 1

Many programs that generate colored output detect if they're writing to a TTY, and switch off colors if they aren't. This is because color codes are annoying when you only want to capture the text, so they try to "do the right thing" automatically.

The simplest way to capture color output from a program like that is to tell it to write color even though it's not connected to a TTY. You'll have to read the program's documentation to find out if it has that option. (e.g., grep has the --color=always option.)

You could also use the expect script unbuffer to create a pseudo-tty like this:

echo barney | unbuffer grep barney | sed -n 1,$\ p

Solution 2

It works for me ;-! (in the current MingW environment)

echo barney | grep --color=always barney | sed -n '1,$p'
**barney**

# barney displays as red text

$ grep --version
GNU grep 2.5.4

$ sed --version
GNU sed version 4.2.1
Share:
8,975

Related videos on Youtube

Peter.O
Author by

Peter.O

Updated on September 18, 2022

Comments

  • Peter.O
    Peter.O over 1 year

    I sometime want to pipe the color-coded output fror a process, eg. grep... but when I pipe it to another process, eg. sed, the color codes are lost...

    Is the some way to keep thes codes intact ?

    Here is an example which loses the colored output:

    echo barney | grep barney | sed -n 1,$\ p   
    
    • Peter.O
      Peter.O about 13 years
      PS.. as general info.. I've just discovered that less can accept colorized text input ( nice :) ...eg: tree -C ~/ |less -R or ls -lR --color=always . |less -R
  • Peter.O
    Peter.O about 13 years
    @shellter... yes, this particular grep example works for me too.. I wasn't aware of that option until you posted the answer, so thanks for that... However. I'm still wondering if there is some general way to do this.... eg tree is colorized a-la dircolors if the LS_COLORS environment variable is set and output is to tty ... Maybe(?) there is a way to trick a pipe into thinking it is outputting to a TTY.. or some such general workaround ..
  • Peter.O
    Peter.O about 13 years
    I've just checked man tree... It too has a similar option, -C ... perhaps it is a common feature for programs that output color escape codes..
  • XGouchet
    XGouchet about 13 years
    thanks for that info about TTY detection and the unbuffer fake-out!
  • Peter.O
    Peter.O about 13 years
    @cjm.. It's not working here (on Ubuntu 10.04 / bash 4.1.5) ...but my 10.04 repo only has a -dev version, and nothing in backports... It's a bit late here, so I'll look again tomorrow... but as you've said, these colorizing programs probably toggle according to the output destination (mhhh but how do they know it is goiong to tty..no matter) .. and they may well have an option to force it.. thanks,,,
  • cjm
    cjm about 13 years
    @fred, they generally use isatty to find out where stdout is going. I'm not actually sure if unbuffer works when you're piping into the program as well as out of it; I don't have it installed here to try.
  • LiuYan 刘研
    LiuYan 刘研 over 10 years
    thanks for the unbuffer information, it help my IRC bot's output like the output in shell.
  • Jags
    Jags almost 3 years
    @cjm How could I use unbuffer with exec so that it preserves colors in console. Command I like to modify is: exec > >(tee $LOG_FILE) 2>&1;. My question on AskUbuntu: https://askubuntu.com/q/1344347/928088. Thank you so much.