Re: SECONDS=0 does not reset SECONDS, or I'm missing something
From:
Tim Visher
Subject:
Re: SECONDS=0 does not reset SECONDS, or I'm missing something
Date:
Thu, 4 Jun 2020 14:57:57 -0400
On Thu, Jun 4, 2020 at 2:39 PM Chet Ramey <chet.ramey@case.edu> wrote:
> On 6/4/20 6:14 AM, Andreas Kusalananda Kähäri wrote:
>
> > #!/usr/local/bin/bash -x
> >
> > for name do
> > SECONDS=0
> > rm -r -f "$name"
> > [[ SECONDS -gt 0 ]] && sleep "$SECONDS"
> > done
> >
> > What I noticed was that the script would go to sleep for a second
> > every second, even if the deletion of directories was quick. This
> > indicates that SECONDS=0 didn't properly reset the SECONDS timer. Or,
> > it indicates that the timer is set by the system's clock (the current
> > second according to the current time), and that it is not a real "timer"
> > at all.
>
> Yes, SECONDS is in seconds granularity, whether that is seconds since the
> epoch or seconds since an assignment to it, as you quoted below. There's
> no expectation of subsecond granularity -- it's just whatever the system
> tells the shell the current number of seconds is.
>
> The next version will use the number of seconds returned from gettimeofday,
> but there's still nothing in there that attempts to round it up or down
> depending on tv_usec.
Thanks for that explanation, Chet. So do I understand it correctly that an
assignment to SECONDS triggers an internal caching of `now` (I assume down
to whatever granularity the underlying system supports) and the value
that's assigned to SECONDS is used as the offset that's added to it? That
would explain the behavior I saw in my experiments, I think, and is
definitely not the mental model I had for SECONDS so that's good to have
uncovered. :)