← All posts

Fix grep color not display when combine with watch command

First, make sure grep colors work at all

grep highlighting a keyword in red

Normally grep highlights your keyword in red. If it doesn’t, add this to your .zshrc or .bashrc.

export GREP_OPTIONS='--color=always'

Does it still work with the watch command?

Let’s do a quick test. This runs every 5 seconds, listing files and filtering for the keyword spring.

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

watch output showing escaped color codes instead of red

Not what you expected?

Instead of red, the terminal shows a bunch of escape codes. Ugly.

The fix is one little flag: –color.

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

watch output with grep colors showing correctly in red

Want it even shorter?

If you don’t feel like typing –color every time, just alias watch in your .zshrc or .bashrc.

alias watch="watch --color"

Then run watch like normal, no color flag needed.

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

Good luck !