Colors with unix command watch?
Some newer versions of watch
now support color.
For example watch --color ls -ahl --color
.
Related.
linux watch -c (with color parameter), but commands display colorless output. Why?
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
bash-program watch and ANSI escape sequences in the output
The --color option does the trick for me.
watch --color -n 1 "echo -e '\033[36mHello World\033[0m'"
Or, in the absence of the color option, how about a home-grown watch:
while [ 1 ]; do clear; echo -e '\033[36mHello World\033[0m'; sleep 1; done
Simple clone of unix watch using Ruby (with coloured output)
You're correct that the commands you are running switch to a non-colour output when invoked from a script. They detect that their standard output is not a terminal and modify their output accordingly.
Fortunately, you should be able to trick the commands into thinking they are outputting to a terminal by using a pseudo-terminal. You can do this using the PTY module in Ruby.
I took the following from this answer and tested it on Ruby 1.9.3-p392.
require 'pty'
PTY.spawn('ls --color=auto') do |stdin, stdout, pid|
begin
stdin.each {|line| print line}
rescue Errno::EIO
# End of input
end
end
How can I make the watch command interpret vt100 sequences?
Edit:
More recent versions of watch
support color. You will need to use an extra level of quoting to preserve the quotes and escapes in the particular situation of the example in the question:
watch 'echo -e "\033[31mHello World\033[0m"'
From man watch
:
-c, --color
Interpret ANSI color sequences.
Previously:
From man watch
:
Non-printing characters are stripped from program output. Use "cat -v"
as part of the command pipeline if you want to see them.
But they don't get interpreted, so I don't think there's any way.
Read Command : Display the prompt in color (or enable interpretation of backslash escapes)
read
won't process any special escapes in the argument to -p
, so you need to specify them literally. bash
's ANSI-quoted strings are useful for this:
read -p $'\e[31mFoobar\e[0m: ' foo
You should also be able to type a literal escape character with Control-v Escape, which will show up as ^[
in the terminal:
read -p '^[[31mFoobar^[[0m: ' foo
OS X Bash, 'watch' command
You can emulate the basic functionality with the shell loop:
while :; do clear; your_command; sleep 2; done
That will loop forever, clear the screen, run your command, and wait two seconds - the basic watch your_command
implementation.
You can take this a step further and create a watch.sh
script that can accept your_command
and sleep_duration
as parameters:
#!/bin/bash
# usage: watch.sh <your_command> <sleep_duration>
while :;
do
clear
date
$1
sleep $2
done
Related Topics
Sending Keyboard Input to a Program from Command-Line
Difference Between Patch and Diff Files
What Happens to Other Processes When a Docker Container's Pid1 Exits
Using Find - Deleting All Files/Directories (In Linux ) Except Any One
Xdotool Commands Bound to Key Shortcuts Doesnot Work
Bash: Add String to the End of the File Without Line Break
How to Get a List of All Valid Ip Addresses in a Local Network
Get Final Url After Curl Is Redirected
Using Grep to Search for a String That Has a Dot in It
How Are Sbrk/Brk Implemented in Linux
Reliability of Linux Kernel Add_Timer at Resolution of One Jiffy
How to Convert ".." in Path Names to Absolute Name in a Bash Script
Can You Enter X64 32-Bit "Long Compatibility Sub-Mode" Outside of Kernel Mode
Sort Logfile by Timestamp on Linux Command Line
Why Is It That Utf-8 Encoding Is Used When Interacting with a Unix/Linux Environment
Bypass Rsync Prompt "Are You Sure You Want to Continue Connecting"