Keep running command until output differs from previous run in Bash

bash

I have a script I want to run every x seconds until the output changes. With a simple while or until loop, I know how to check the output for a particular string using grep, but what I want to do is continue iterating the loop until the output from the current iteration does not equal the output from the previous iteration.

How can I achieve this in bash?

The closest I've gotten is below, where the command is run twice in each iteration, but this produces extra runs because the while loop cannot remember the output from the previous iteration.

while [ "$(command)" = "$(command)" ]; do sleep 10m; done

Best Answer

From man 1 watch:

-g, --chgexit
Exit when the output of command changes.

watch is not required by POSIX but it's quite common anyway. In Debian or Ubuntu it's in the procps package along with kill and ps (and few other tools).

Example:

watch -g -n 5 'date +%H:%M'
Related Question