15

For example, I'd like to color the output of the locate command so it's easily distinguished from the other terminal text.

It should work something like this:

locate -bir pdf | some_command_to_color_the_result

Coloring shouldn't be limited for the locate command only: I need a generic solution that colors text using pipelines, for example feeding it the output of grep or cat.

If there's no built-in Linux command, any hints on how to create one are appreciated.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
user3158243
  • 191
  • 1
  • 1
  • 5
  • 2
    grep --color will provide colour – V H Jan 03 '14 at 18:05
  • It depends on what you want coloured, but `grep --color '.'` will give you everything in some colour (red when I try it). – Jonathan Leffler Jan 03 '14 at 18:08
  • This answered my question. I created an alias "alias color='grep --color .", then I use it like "locate -bir pdf | color" – user3158243 Jan 03 '14 at 18:20
  • Possible duplicate: *[How do I output coloured text to a Linux terminal?](https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal)* – Peter Mortensen Jun 11 '21 at 23:01
  • @PeterMortensen The target you've suggested is for C++. This is a bash question. – cigien Jun 11 '21 at 23:58
  • @JonathanLeffler : you could also color specific things while still showing everything by using a simple trick: `egrep --color '^|regex'` # for exemple: `ps -ef | egrep '^|user1|bash'` shows the whole output but colors "user1" and "bash" as well. Of course this introduces escape codes at the beginning of the line (and around the regexes), but if you `... | cat` (or `... > resultfile` ) they won't be colored anymore (as it is not --color=always) and therefore won't mess up further processing – Olivier Dulac Sep 30 '22 at 12:33

10 Answers10

16

You can use escape sequences to change the font color of any output to the bash shell. Here are some of the color codes you'll need:

BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
PINK="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
NORMAL="\033[0;39m"

Once these are defined, you can use them in normal echo commands. For instance:

echo -e $GREEN this text is green $NORMAL and this is normal

Note that the -e is not always necessary, but on some OSs (incl. osx) is required for to enable escape sequences.

Given these definitions you can build scripts and pipes to color the output from other commands. Here is a complete example I use to color the output from svn up:

#!/bin/bash

BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
PINK="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
NORMAL="\033[0;39m"

TMPFILE=.cvsup.tmp

svn up > $TMPFILE
svn status >> $TMPFILE
printf $YELLOW
grep -e ^"\? " -e ^"I " $TMPFILE
printf $GREEN
grep -e ^"R " -e ^"U " -e ^"G " $TMPFILE
printf $BLUE
grep -e ^"M " -e ^"E " $TMPFILE
printf $RED
grep -e ^"C " -e ^"! " -e ^"X " -e ^"~ " $TMPFILE
printf $PINK
grep ^"R " $TMPFILE
printf $PINK
grep ^"D " $TMPFILE
printf $CYAN
grep ^"A " $TMPFILE

printf $NORMAL
rm $TMPFILE

You can also look at tput.

Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
10
_Esc_="$( printf '\033' )"
_norm_="${Esc}[0m" #returns to "normal"
_bold_="${Esc}[0;1m" #set bold
_red_="${Esc}[0;31m" #set red
_boldred_="${Esc}[0;1;31m" #set bold, and set red.

somecommand | sed -e "s/someregexp/${_boldred_}&${_norm_}/g"  # will color any occurence of someregexp in Bold red

printf "%s" "$_red_" ; locate something ; printf "%s" "$_norm_"  # will color output of locate something in red
   #I (ab)use printf "%s" "something", as it's more portable than echo,and easy to modify

There are many other ways (create a function/script that can colorize a regexp, for example, and then : somecommand | colorize -c _green_ 'foo.*bar' 'other' )

Olivier Dulac
  • 3,695
  • 16
  • 31
10

The main tool for that is of course lolcat!

locate -bir pdf | lolcat

enter image description here

To install:

sudo apt install lolcat 

See man lolcat for customizations.

NVRM
  • 11,480
  • 1
  • 88
  • 87
5

As suggested by Jonathan Leffler, comment posted as an anwser:

grep --color will provide colour

V H
  • 8,382
  • 2
  • 28
  • 48
4

The following answered my question:

1- I create an alias in my .bashrc

alias color='grep --color .'

2- Then whenever I want to color the pipeline text output I use color alias like:

locate -bir pdf | color

This will color the output to red

user3158243
  • 191
  • 1
  • 1
  • 5
4

I prefer use highlight utility:

highlight -O xterm256 -S sh

-S sh here means treats the input as shell script syntax.

More info: http://www.andre-simon.de/

I set it as an alias through ~/.bashrc: enter image description here

enter image description here

林果皞
  • 7,539
  • 3
  • 55
  • 70
2

There is a far better way to achieve customizable coloring:

colorit

You can use it as shown in other answers via some_command | colorit but it is nicely configurable over the .coloritrc. In mine I have stuff like

dnl  Define some useful color variables
define(`red', `1')
define(`green', `2')
define(`magenta', `5')
dnl
dnl  Mark macro arguments: regexp foreground-color [background-color]
dnl
define(`mark', ``mark "$1"'' `ifelse(`$#', `3', ``"\033[3$2;4$3m"'',
``"\033[3$2m"'')' `"\033[m"')
dnl
divert
mark(`warning', magenta)
mark(`Warning', magenta)
mark(`Traceback', magenta)
mark(`Error', red)
mark(`FAIL', red)
mark(`ERROR', red)
mark(`XFAIL', green)
mark(`ok', green)
mark(`OK', green)
mark(`PASS', green)

and use it all the time for coloring compiler output and similar stuff. See my .coloritrc for more.

bijancn
  • 462
  • 5
  • 11
1

I think the hl command available on git hub might help you :
have a look at http://www.flashnux.com/notes/page_000022_US.html

Bush
  • 141
  • 1
  • 2
1

You should have a look at the hl command available on git hub:

git clone http://github.com/mbornet-hl/hl

and on :

http://www.flashnux.com/notes/page_000022_US.html

hl is a Linux command written in C, especially designed to color a text file or the output of a command. You can use up to 42 colors simultaneously, and use a configuration file to simplify command lines. You can colorize the output of every command that can be piped to another one. And if you know what regular expressions are, it will be very easy for you to use. You can use the man page to understand how to use it.

rkta
  • 3,959
  • 7
  • 25
  • 37
Bush
  • 141
  • 1
  • 2
0

Use the tput command.

Most terminals support 8 foreground text colors and 8 background colors (though some support as many as 256). Using the setaf and setab capabilities, we can set the foreground and background colors. The exact rendering of colors is a little hard to predict. Many desktop managers impose "system colors" on terminal windows, thereby modifying foreground and background colors from the standard. Despite this, here are what the colors should be:

Value Color

0 Black

1 Red

2 Green

3 Yellow

4 Blue

5 Magenta

6 Cyan

7 White

8 Not used

9 Reset to default color

Actual example: set color to red, cat and then change color back:

tput setaf 1; cat /proc/meminfo ; tput setaf 9