Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash command preserve color when piping [duplicate]

Possible Duplicate:
Can colorized output be captured via shell redirect?

setup

In this case specifically I'm trying to preserve the colors in git status -s when piping it to another command.

Some git commands, diff for instance, and other commands like grep have an option --color=always but git status does not.

question

Is there a way to pipe or capture the output of a command and make it think it is outputting to the xterm shell so it doesn't automatically disable colors?

like image 738
rennat Avatar asked Oct 03 '11 22:10

rennat


1 Answers

Here's a script snippet using the colorized output of ls as an example (on Mac OS X 10.6).

# no colored ls output if stdout is a pipe (and not a tty) ls -G / ls -G / | cat script -q /dev/null ls -G / | tr -d '\r' | cat  # write output of script command to a variable var="$(script -q /dev/null ls -G / | tr -d '\r' | cat)" echo "$var" 
like image 87
vego Avatar answered Oct 14 '22 00:10

vego