Bash Command Standard Output String Colorize

Colorizing Standard Output Strings in Bash Commands

In Bash, the command-line language on Linux PCs and servers, this article introduces how to colorize (highlight) search strings or specific strings (such as error messages, warning messages, etc.) in standard output. A common problem with output from installation commands like pip is when there's an error but you can't figure out where the error is. When it's stdout instead of stderr, color coding isn't done automatically...

Shou Arisaka
2 min read
Nov 24, 2025

In Bash, the command-line language on Linux PCs and servers, this article introduces how to colorize (highlight) search strings or specific strings (such as error messages, warning messages, etc.) in standard output.

A common problem with output from installation commands like pip is when there’s an error but you can’t figure out where the error is. When it’s stdout instead of stderr so color coding isn’t done automatically, or when the string is not error but failure, failed, lost or such strings, at least if they were highlighted it would be easier to find.

Do it like this:

ls | tail -3 | GREP_COLOR='01;31' grep --color -P "zen|"
# or
# ls | tail -3 | grep --color -P "zen|"

An example of highlighting the string zen in red.

The key is the zen| part. With just zen, grep’s specification is that lines that don’t contain zen won’t be output. This prevents that. As mentioned later, zen|$ apparently works too. (Unverified) If you want to be explicit, this method seems clearer.

Color specific words in Linux terminal whenever they appear - Stack Overflow

Image

There seems to be this method too. However, I’m not familiar with egrep so I haven’t verified it. (I suspect it’s probably regex by default.)

tail -f myfwlog | GREP_COLOR='01;36' egrep --color=always 'ssh|$' | GREP_COLOR='01;31' egrep -i --color=always 'drop|deny|$'

bash - Grep output with multiple Colors? - Stack Overflow

Also refer to this:

egrep --color "\b(PASS|FAIL)\b|$"

How do you colorize only some keywords for a bash script? - Unix & Linux Stack Exchange

You can find bash color codes here: 32 is green, 31 is red, etc.

bash:tip_colors_and_formatting - FLOZz’ MISC

Alias example. Note that since red makes it hard to tell which output it is, the color is set to 36 Cyan.

alias colorizeError="GREP_COLOR='01;36' grep --color -P 'deny|failure|failed|error|lost|'"

Share this article

Shou Arisaka Nov 24, 2025

🔗 Copy Links