Get color output in bash

2020-02-02 12:40发布

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

标签: bash
5条回答
贪生不怕死
2楼-- · 2020-02-02 12:53

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.)

查看更多
家丑人穷心不美
3楼-- · 2020-02-02 13:01

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
查看更多
成全新的幸福
4楼-- · 2020-02-02 13:06

Installing and using colormake is another simple option.

 sudo apt-get install colormake
查看更多
太酷不给撩
5楼-- · 2020-02-02 13:08

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.

查看更多
狗以群分
6楼-- · 2020-02-02 13:10

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

查看更多
登录 后发表回答