How can I display the run count as part of the `watch` output in a terminal?

I'd like to see a run counter at the top of my watch output.

Ex: this command should print a count value which increments every 2 seconds, in addition to the output of my main command, which, in this case is just echo "hello" for the purposes of this demonstration:

export COUNT=0 && watch -n 2 'export COUNT=$((COUNT+1)); echo "Count = $COUNT" \
&& echo "hello"'

But, all it outputs is this, with the count always being 1 and never changing:

Count = 1
hello

How can I get this Count variable to increment every 2 seconds when watch runs the command?



Read more here: https://stackoverflow.com/questions/64405980/how-can-i-display-the-run-count-as-part-of-the-watch-output-in-a-terminal

Content Attribution

This content was originally published by Gabriel Staples at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: