1

watch -n1 $() does not update $().

what is the workaround?

here is my example:

watch -n1 echo $(( $(date +%s -d "sun") - $( date +%s ) )) 

this results in

Every 1.0s: echo 106602 106602 

the expected output should have been:

Every 1.0s: echo $(( $(date +%s -d "sun") - $( date +%s ) )) 106602 

with 106602 being reduced every second

2
  • It likely does run every interval - but it's producing the same value each time because the arithmetic expression is evaluated before watch is invoked Commented Jan 13, 2023 at 21:59
  • how can I make it be evoked by watch instead of before it? Commented Jan 13, 2023 at 22:02

1 Answer 1

4

It likely does run every interval - but it's overwriting the same value every time because the arithmetic expression is evaluated by your interactive shell before it is passed to the watch command. You can see this if you run pgrep from another terminal:

watch -n1 echo $(( $(date +%s -d "sun") - $( date +%s ) )) 

then

$ pgrep -af watch 81 watchdogd 29311 watch -n1 echo 111748 

You can prevent early evaluation by single-quoting the expression:

watch -n1 echo '$(( $(date +%s -d "sun") - $( date +%s ) ))' 

giving

$ pgrep -af watch 81 watchdogd 29543 watch -n1 echo $(( $(date +%s -d "sun") - $( date +%s ) )) 

which will pass the expression to a /bin/sh shell by default.

1
  • this fixes the problem. Commented Jan 13, 2023 at 22:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.