Linux command color output

For example, I would like to colorize the output of the locate command so that it can be easily distinguished from the text of another terminal.

This should work something like this:

 locate -bir pdf | some_command_to_color_the_result 

Color should not be limited only to the locate command: I need a general solution that colors text using pipelines, for example, feeds grep or cat output to it.

If there is no Linux built-in command, any advice on how to create it is welcome.

+10
source share
9 answers
 norm="$(printf '\033[0m')" #returns to "normal" bold="$(printf '\033[0;1m')" #set bold red="$(printf '\033[0;31m')" #set red boldred="$(printf '\033[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 more portable than echo,and easy to modify 

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

+10
source

You can use escape sequences to change the font color of any output to the bash shell. Here are some color codes you will 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 they are defined, you can use them in regular echo commands. For instance:

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

Note that -e is not always necessary, but for some operating systems (including osx) it is required to enable escape sequences.

Based on these definitions, you can create scripts and channels for color output from other commands. Here is a complete example that I use for color 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 see tput .

+7
source

As suggested by Jonathan Leffler, the comment was posted as anwser:

grep --color will provide color

+4
source

I prefer to use the highlight utility:

 highlight -O xterm256 -S sh 

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

Additional information: http://www.andre-simon.de/

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

enter image description here

+4
source

The following question answered my question:

1- I create an alias in my .bashrc

 alias color='grep --color .' 

2- Then whenever I want to colorize the output of pipeline text, I use a color alias, for example:

 locate -bir pdf | color 

This will turn the output color to red.

+2
source

There is a much better way to achieve custom coloring:

colorit

You can use it, as shown in other answers, via some_command | colorit some_command | colorit , but it is highly configurable over .coloritrc . I have things 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 the output of the compiler and similar materials. See .coloritrc for more details.

+1
source

I think the hl command available in the git hub can help you:
take a look http://www.flashnux.com/notes/page_000022_US.html

+1
source

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

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

and further:

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

hl is a Linux command written in C specially designed to color a text file or output a command. You can use up to 42 colors at a time and use the configuration file to simplify the command line. You can colorize the output of each command that can be passed to another. And if you know what regular expressions are, it will be very easy for you to use. You can use the help page to understand how to use it.

+1
source

The main tool for this, of course, is lolcat !

 locate -bir pdf | lolcat 

enter image description here

For installation:

 sudo apt install lolcat 

See man lolcat for configuration.

+1
source

All Articles