All about watch colors: The ultimate simple style tips.

by Alice Browne

Okay, so I wanted to mess around with the `watch` command in Linux today. It’s something I’ve heard about but never really used. My goal? To keep an eye on the colors in a terminal output that changes over time. Sounds kinda niche, but I found it pretty interesting!

All about watch colors: The ultimate simple style tips.

Getting Started

First, I needed something that actually produced changing colors. I figured a simple script that spits out colored text would do the trick. I remembered some basic ANSI escape codes for colors, so I whipped up a tiny shell script:

bash

while true; do

echo -e “e[31mRede[0m e[32mGreene[0m e[34mBluee[0m”

sleep 0.5

All about watch colors: The ultimate simple style tips.

done

What’s this do? Basically, it loops forever. Inside the loop, it prints the words “Red”, “Green”, and “Blue”, each in their respective color. The `e[31m` stuff is the ANSI code for red, `e[32m` is green, `e[34m` is blue, and `e[0m` resets the color back to normal. The `-e` flag for `echo` tells it to interpret those escape sequences. The `sleep 0.5` just pauses for half a second so it’s not a crazy fast blur.

I saved this as `color_*` and made it executable with `chmod +x color_*`.

Enter the `watch` Command

Now for the main event! I ran the script using `watch`:

bash

All about watch colors: The ultimate simple style tips.

watch ./color_*

Boom! My terminal was now showing the “Red Green Blue” text, cycling through the colors, and updating every 2 seconds (that’s the default `watch` interval). Pretty neat, but I wanted it to update faster, matching the script’s `sleep` interval.

Tweaking the Interval

That’s where the `-n` option comes in. I adjusted the command:

bash

watch -n 0.5 ./color_*

All about watch colors: The ultimate simple style tips.

Now `watch` was refreshing every 0.5 seconds, perfectly in sync with my script’s output. Much better! The colors were changing smoothly.

Highlighting the Differences

Then I got curious. `watch` has a `-d` option that’s supposed to highlight the differences between updates. I figured, why not?

bash

watch -n 0.5 -d ./color_*

This was really cool! Instead of just flashing the whole line, `watch` now only highlighted the parts that changed. Since the colors were constantly changing, those words were constantly highlighted. It really emphasized the changes.

All about watch colors: The ultimate simple style tips.

Going a Little Further (and Failing…at First)

I thought, “Can I watch only part of the output?”. I tried using `grep` within the `watch` command:

bash

watch -n 0.5 ” ./color_* grep Red”

…That didn’t work as expected. It seems that grep is not working inside watch directly. I will keep working on it next time. Maybe I should try different aproaches.

Then I tried with awk:

All about watch colors: The ultimate simple style tips.

bash

watch -n 0.1 ‘./color_* awk “{print $1}”‘

Yeah! It works, It only print the “Red” word and highlight the color’s change!

Wrapping Up

So, that was my little adventure with `watch` and colors. It’s a simple tool, but surprisingly versatile. I can see how it would be super useful for monitoring log files, system resources, or anything else that changes over time. The highlighting feature is especially handy. My attempt to filter the output directly with grep was not so successful, But I find another way by using awk, It was a fun little experiment, and I definitely learned a few things!

You may also like

Leave a Comment