Colors with unix command "watch"?

LinuxBashUbuntuWatch

Linux Problem Overview


Some commands that I use display colors, but when I use them with watch the colors disappears:

watch -n 1 node file.js

Is it possible to have the colors back on somehow?

Linux Solutions


Solution 1 - Linux

Some newer versions of watch now support color.

For example watch --color ls -ahl --color.

Related.

Solution 2 - Linux

Do not use watch ... When you use watch programs can detect they're not writing to a terminal and then strip the color. You must use specific program flags to keep the control codes there.

If you don't know the flags or there isn't you can make a poor's man watch by:

while sleep <time>; do clear; <command>; done

It will have a bit of flicker (watch works "double buffered") but for some stuff it is useful enough.

You may be tempted to make a double buffered poor man's watch using

while sleep <time>; do <command> > /tmp/file; clear; cat /tmp/file; done

But then you'll hit again the "I am not writing to a terminal" feature.

Solution 3 - Linux

You can duplicate the fundamental, no-frills operation of watch in a couple lines of shell script.

$ cat cheapwatch 
#!/bin/sh

# Not quite your Rolex

while true ; do
  clear
  printf "[%s] Output of %s:\n" "$(date)" "$*"
  # "$@" <- we don't want to do it this way, just this:
  ${SHELL-/bin/sh} -c "$*"
  sleep 1  # genuine Quartz movement
done

$ ./cheapwatch ls --color  # no problem

Eventually, someone very clever will hack a tr command into this script which strips control characters, and then force the user to use --color to disable that logic. For the time being, the sheer naivete of this implementation is keeping the color-eating monster away.

If you're in a situation where watch doesn't have the --color option and you can't upgrade the package for whatever reason, maybe you can throw this in.

Solution 4 - Linux

While other answers solve this problem, the easiest way to accomplish this is using the unbuffer tool. To use it simply do:

$ watch --color 'unbuffer <your-program>'

This way you don't have to hunt for control sequence enabling flags of your program. The caveat however is that your version of watch should support the --color flag.

You can install unbuffer on Debian or Ubuntu using sudo apt-get install expect.

Solution 5 - Linux

YES

watch works with color output. it is part of the procps package (at least in debian) here is bugreport for your question http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=129334 where they answer, that you should update the procps package

e.g. with ubuntu 11.04 this package works http://packages.debian.org/wheezy/procps

tl;dr

update procps

Solution 6 - Linux

From watch manual:

>Non-printing characters are stripped from program output. Use "cat -v" as part of the command pipeline if you want to see them.

Though, I am not sure how to use it.

Solution 7 - Linux

Other answers might not work if the command does not have option to force color output. Or you might be lazy like me and do not want to browse manual for each command to find the correct setting. I tried a few different techniques:

The script command

Script command captures output as run by an interactive terminal session. With the combination of --color parameter of watch, it retains colors:

watch --color "script -q -c '<command>' /dev/null"

-q is for quiet, -c is for command and /dev/null is the log file, which is not needed as stdout also shows the output.

Edit: This is the best option so far, I left the earlier solution below for the interested.

Earlier try: Rewrite the terminal window

As suggested by some, a simple while loop with clear and sleep can be used to run the command in terminal without capturing its output. This usually causes flicker, as clear removes all characters and then the command take some time to print the new output line by line.

Fortunately, you can fix this with some clever terminal tricks using tput. Just leave the old output visible while writing the new on top.

Here is the script:

#!/bin/sh
trap "tput cnorm" EXIT  # unhide the cursor when the script exits or is interrupted

# simple interval parameter parsing, can be improved
INTERVAL=2s
case $1 in
  -n|--interval)
    INTERVAL="$2"
    shift; shift
  ;;
esac

clear           # clear the terminal
tput civis      # invisible cursor, prevents cursor flicker

while true; do
  tput cup 0 0  # move cursor to topleft, without clearing the previous output
  sh -c "$*"    # pass all arguments to sh, like the original watch
  tput ed       # clear all to the end of window, if the new output is shorter
  sleep "$INTERVAL"
done

The script fixes color problems, but a different bug remains: If the lines of the command output gets shorter, the rest of the line is not necessarily erased, so the output might not match with reality!

Solution 8 - Linux

unbuffer is a great way to avoid process knowing is writing to TTY, but worth noting that watch does not support 8-bit colors and above yet.

If instead of ls you use something more modern like bat or exa, you should append --theme=ansi (not even --theme=base16 works). git log works out of the box because it always uses 3-bit colors (source).

Example:

watch --color -d -n 0.5 'mycommand file | bat --color=always --theme=ansi'

Could also use -f instead of --color=always.

Another alternative could be Pygments.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnever_had_a_nameView Question on Stackoverflow
Solution 1 - LinuxDennis WilliamsonView Answer on Stackoverflow
Solution 2 - LinuxtheistView Answer on Stackoverflow
Solution 3 - LinuxKazView Answer on Stackoverflow
Solution 4 - LinuxlolView Answer on Stackoverflow
Solution 5 - LinuxdavidDavidsonView Answer on Stackoverflow
Solution 6 - LinuxBenoitView Answer on Stackoverflow
Solution 7 - LinuxTK009View Answer on Stackoverflow
Solution 8 - LinuxPablo BianchiView Answer on Stackoverflow