Fix grep color not display when combine with watch command

Natthapon Pinyo
2 min readDec 14, 2022

--

Make sure that grep color is working

In general, grep should highlight the keyword with red color.
Otherwise, you need to add this variable to your .zshrc or .bashrc

export GREP_OPTIONS='--color=always'

Is it work when combine with watch command?

Simple test by adding watch command to execute every 5 seconds for listing and filter file name with desired keyword spring

$ watch -n 5 "ls -la | grep spring"

Not as expected?

The terminal displayed escaped code instead of red color.

To fix, add parameter named --color

$ watch --color -n 5 "ls -la | grep spring"

Easy enough? No?

You can alias watch command to pre-combined with color for shorter syntax in your .zshrc or .bashrc

alias watch="watch --color"

Then simply run watch command without define color parameter

$ watch -n 5 "ls -la | grep spring

Good luck !

--

--

Natthapon Pinyo
Natthapon Pinyo

No responses yet