1

When I do 'ls', or 'service some_service_name status', or any other command with colored output, it, "surprisingly" :), shows it's output in color, but when I run them with 'watch' and even with 'watch -c' command with a color enabling parameter, they still show without colors.

Why?, and how to fix it?

Legionar
  • 7,472
  • 2
  • 41
  • 70
igoryonya
  • 167
  • 3
  • 12

1 Answers1

1

ls and others see that they are run from a script so they go to the default mode (without colors):

With --color=auto, ls emits color codes only when standard output is connected to a terminal.

try forcing the color output:

watch -c ls --color
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • OK, I see, It worked for ls, but is there a color option for 'service' command? 'man service' has nothing about it. – igoryonya Jul 13 '16 at 07:53
  • I've found that 'service' command is just a bash script executing systemctl, but 'man systemctl' doesn't show anything about color either. – igoryonya Jul 13 '16 at 08:04
  • those commands check if it is an interactive (a tty) or non-interactive and don't show colors in the other case, I'm not sure if there is a way to force them to show always – Krzysztof Krasoń Jul 13 '16 at 08:05
  • And here's how to pretend a tty: http://stackoverflow.com/questions/32910661/pretend-to-be-a-tty-in-bash-for-any-command – Krzysztof Krasoń Jul 13 '16 at 08:07
  • Thanx, this gives me the solution to the problem. The faketty, in the example, didn't work for me, but @ least, I know now which direction to look at and I will read some information about tty now. – igoryonya Jul 13 '16 at 08:34
  • some commands accept `--ansi` to force ANSI output (eg coloured text) `watch -cn1 "git ls-files -m | xargs phplint --ansi --no-progress -w src/"` – Jerram Feb 09 '22 at 20:43