Simple way to colour alternate output lines in bash Simple way to colour alternate output lines in bash shell shell

Simple way to colour alternate output lines in bash


Not very pretty but does the trick:

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

#!/bin/bashwhile read linedo  echo -e "\e[1;31m$line"  read line  echo -e "\e[1;32m$line"doneecho -en "\e[0m"

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


Perl is installed on many systems. You could have it alternate for you:

grep -r whatever somedir/ | perl -pe '$_ = "\033[1;29m$_\033[0m" if($. % 2)'

In Perl $. can be substituted with $INPUT_LINE_NUMBER if you prefer readability.


This is to delineate wrapped lines I presume?This shell script uses a background color from the 256 color palette,so as not to interfere with other highlighting that grep --color might do.

#!/bin/shc=0while read line; do  [ $(($c%2)) -eq 1 ] && printf "\033[48;5;60m"  printf "%s\033[0m\n" "$line"  c=$(($c+1))done

This has the caveat that backslashes etc. within the line will be mangled,so treat this as pseudo code for reimplementation