43

Whenever I use grep, and I pipe it to an other program, the --color option is not respected. I know I could use --color=always, but It also comes up with some other commands that I would like to get the exact output of that command as the output I would get if I was in a tty.

So my question is, is it possible to trick a command into thinking that the command is run inside a tty ?

For example, running

grep --color word file # Outputs some colors
grep --color word file | cat # Doesn't output any colors

I'd like to be able to write something like :

IS_TTY=TRUE grep --color word file | cat  # Outputs some colors

This question seems to have a tool that might do what I want :empty - run processes and applications under pseudo-terminal (PTY), but from what I could read in the docs, I'm not sure it can help for my problem

5
  • 1
    you realize that --color changes the actual data stream that is sent to command further down the pipeline? The consequence would be breaking other-wise reasonable code because searchTarget is not ^[32;4gsearchTarget^[32;h (or similar). Good luck.
    – shellter
    Oct 2, 2015 at 16:17
  • 1
    Yes I realize that, I will in my scripts never use that technique to color greps, but some commands that I use seem to output something different when used outside a tty (for example ag , the silversearcher, but also mocha, ...) and I don't have time to learn all the options to print with the same format whenever I pipe those commands into others)
    – edi9999
    Oct 2, 2015 at 16:34
  • Hmmm.. I think this has come up before with some surprising answers (to me). Did you search much for [linux] (or) [bash] --color? Good luck.
    – shellter
    Oct 2, 2015 at 16:51
  • I searched a bit, but haven't found anything that would solve my issue.
    – edi9999
    Oct 3, 2015 at 9:33
  • 3

1 Answer 1

47

There are a number of options, as outlined by several other Stack Overflow answers (see Caarlos's comment). I'll summarize them here though:

  1. Use script + printf, requires no extra dependencies:

     0<&- script -qefc "ls --color=auto" /dev/null | cat
    

    Or make a bash function faketty to encapsulate it:

     faketty () {
         script -qefc "$(printf "%q " "$@")" /dev/null
     }
     faketty ls --color=auto | cat  
    

    Or in the fish shell:

     function faketty
         script -qefc "(printf "%q " "$argv")" /dev/null
     end
     faketty ls --color=auto | cat 
    

    (credit goes to this answer)

    http://linux.die.net/man/1/script

  2. Use the unbuffer command (as part of the expect suite of commands), unfortunately this requires an extra package install, but it's the easiest solution:

     sudo apt-get install expect-dev   # or brew install expect
     unbuffer -p ls --color=auto | cat  
    

    Or if you use the fish shell:

     function faketty
         unbuffer -p $argv
     end
     faketty ls --color=auto | cat 
    

    http://linux.die.net/man/1/unbuffer

This is a great article on how TTYs work and what Pseudo-TTYs (PTYs) are, it's worth taking a look at if you want to understand how the linux shell works with file descriptors to pass around input, output, and signals. http://www.linusakesson.net/programming/tty/index.php

18
  • 2
    Add -e to script to return the return value of the command. Also, consider piping to less -FXur which will display colours and not do anything if the output fits on a single screen (and fix a \r issue if you have it).
    – Tom Hale
    Sep 6, 2016 at 4:58
  • 4
    Swop the c and the e script -qfec "$(printf "%q " "$@")" Jan 30, 2019 at 10:03
  • 1
    @StevieD I just hit the same issue and edited to make this change. Apr 3, 2019 at 21:39
  • 2
    See stackoverflow.com/a/60279429/1356047 for a version that works on both Linux & MacOs AND supports returning status code Feb 18, 2020 at 11:06
  • 1
    It must have been a while since this answer was written, but I just checked the install size of expect on archlinux and it is less than half 1MB
    – smac89
    Jun 19, 2021 at 20:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.