10

I have a script that I'm trying to port from Linux to Darwin/OS X. The Linux version currently depends on the watch command, which doesn't appear to be installed on Darwin/OS X by default. What's the native alternative?

14

There is no native alternative. You must acquire watch from using Homebrew (brew install watch) or MacPorts (port install watch) if you require an actual executable.

You can however, emulate the function of watch. That can be accomplished in a standard bash while loop (from Stack Overflow by Daniel Pittman):

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

That, and other options, are available on Stack Overflow.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .