17

when compiling some projects on linux terminal, I get usually a long output consisting of a lot of information. Usually this information is MONOCHROME. I wonder if bash can be modified somehow, so in all outputs or in some specific outputs (like from Makefile, etc) I can get different colors dependeing on, for instance:

make[1]: Leaving directory 

or

g++ -DHAVE_CONFIG_H     -I. 

etc.

Thanks

Open the way
  • 26,225
  • 51
  • 142
  • 196

5 Answers5

14

I found that in bash tput setf doesn't work. I found this commands for bash that are working well

handy tput commands

tput bold - Bold effect
tput rev - Display inverse colors
tput sgr0 - Reset everything
tput setaf {CODE}- Set foreground color, see color {CODE} below
tput setab {CODE}- Set background color, see color {CODE} below
Colors {code} code for tput command

Color {code}    Color
0    Black
1    Red
2    Green
3    Yellow
4    Blue
5    Magenta
6    Cyan
7    White
Fabio
  • 18,856
  • 9
  • 82
  • 114
  • `tput` is a great little tool with many [other commands](http://stackoverflow.com/a/20983251/24874) for controlling the terminal state and contents in shell scripting. – Drew Noakes Jan 07 '14 at 22:31
12

Sure, just use Bash functions, like, say this one:

make()
{
  pathpat="(/[^/]*)+:[0-9]+"
  ccred=$(echo -e "\033[0;31m")
  ccyellow=$(echo -e "\033[0;33m")
  ccend=$(echo -e "\033[0m")
  /usr/bin/make "$@" 2>&1 | sed -E -e "/[Ee]rror[: ]/ s%$pathpat%$ccred&$ccend%g" -e "/[Ww]arning[: ]/ s%$pathpat%$ccyellow&$ccend%g"
  return ${PIPESTATUS[0]}
}

(Originally via Highlight Warnings in Make.)

Jeff Walden
  • 7,008
  • 2
  • 38
  • 55
5

You can do this portably by using the tput command and the terminfo(5) database. For example,

tput setf 5

with terminal as standard out, will set the foreground color to purple (or something like it; I'm color blind). tput setf 0 resets the foreground color to the default.

For more information, look up terminfo.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
1

Installing and using colormake is another simple option.

 sudo apt-get install colormake
Doruk
  • 385
  • 2
  • 12
0

It seem more like you want the colorized output from make(1). In that case, I'd recommend patches for gnu make from http://git.goodpoint.de/?p=make.git;a=shortlog;h=refs/heads/color-v5.1 described on the ML: http://old.nabble.com/-rfc--Colorized-output-for-GNU-make--td32547742.html

mmaruska
  • 578
  • 4
  • 8