Search code examples
linuxcolorsterminal

Color specific words in Linux terminal whenever they appear


I have a bunch of scripts running in my terminal (and I don't have the ability to edit them) which output messages to the terminal. I would like my terminal to automatically color specific words in the output.

For example, some of the scripts output FAIL when a test fails. How can I configure the terminal to color this specific word, any time it appears, to be in a specific color (for example, red).


Solution

  • It's probably easier to colour the words yourself, rather than getting the terminal to colour them for you. If you can't edit the scripts that create the output, can you filter them through something else?

    At the most likely to be available end of the scale you could pipe your output through grep:

    tail -F logfile | grep --color -P "FAIL|"
    

    This matches either "FAIL" or "", and highlights the matched portion of the string.

    You could further use something more specialised, as described in this blog post, for example.