Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to colour alternate output lines in bash

I have need to grep an entire directory for a string, and I get about 50 results. I would like to colour each second line, either text colour or background colour. Best would be a script that I can pipe the output of any command to, and so that it spits out the same (albeit coloured) output.

like image 670
naught101 Avatar asked Dec 02 '09 08:12

naught101


1 Answers

Not very pretty but does the trick:

(save this to foo.bash and do grep whatever wherever | ./foo.bash)

#!/bin/bash
while read line
do
  echo -e "\e[1;31m$line"
  read line
  echo -e "\e[1;32m$line"
done
echo -en "\e[0m"

Here you can find the list of color codes in bash.

like image 134
Kimvais Avatar answered Oct 03 '22 06:10

Kimvais