Bash: Highlight Matched Texts With Colors
Logs are valuable sources to identify issues. It would be continuously generated (e.g. adb logcat
) or stored as a file. Mostly, I use the terminal to check those. When I check that, it is often not easy to see since every text has the same color. grep
could help to filter out. With --color=auto
or --color=always
option, it supports highlighting texts as well. However, there are cases I don’t want to filter out but want to highlight certain texts or words to check easily. This happens more frequently when I check continuous logs.
After some investigation, I’ve figured out how to do it in bash. By adding the below function, you can highlight a matched text with a specified color.
.bash_profile
The actual showing color is determined by terminal settings. For example, iTerm defines colors in Profiles > Colors.
Here is an example on how to use highlight
and how the result looks.
Though this function works well, it requires quite a long command if I want to highlight multiple texts, as you can see above. Specifying a color is great, but it becomes somewhat cumbersome if it is needed every time. All I need is just to highlight texts with different colors. Based on the highlight
function, the below helper function simplifies it.
.bash_profile