Here's one that uses the --color
option and allows you to use the -n
argument to specify a refresh interval.
swatch_usage() {
cat <<EOF >&2
NAME
swatch - execute a program periodically with "watch". Supports aliases.
SYNOPSIS
swatch [options] command
OPTIONS
-n, --interval seconds (default: 1)
Specify update interval. The command will not allow quicker than
0.1 second interval.
EOF
}
swatch() {
if [ $# -eq 0 ]; then
swatch_usage
return 1
fi
seconds=1
case "$1" in
-n)
seconds="$2"
args=${*:3}
;;
-h)
swatch_usage
return 1
;;
*)
seconds=1
args=${*:1}
;;
esac
watch --color -n "$seconds" --exec bash -ic "$args || true"
}
I only needed color and timing support,
but I'm sure you could add more if you wanted.
The meat of the function is that it executes your command with bash directly in interactive mode and can thus use any aliases or commands that are normally available to you in bash.
I'm not that experienced with scripting so fair warning, your mileage may vary. Sometimes I have to press Ctrl+C a few times to get it to stop, but for what it's worth, I've been using it frequently for 6 months without issue.
Gist form: https://gist.github.com/ablacklama/550420c597f9599cf804d57dd6aad131
"watch -x bash -i -c ll"
(the "-x" tells watch not to use it's own "sh -c" to execute the given command.) But it successfully runs 'll' once, then backgrounds and stops the process. I don't know why.