bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Potentially misleading documentation of SECONDS variable


From: Bash-help
Subject: Potentially misleading documentation of SECONDS variable
Date: Tue, 06 Aug 2024 17:17:15 +0200
User-agent: K-9 Mail for Android

Reading the manual at
<https://www.gnu.org/software/bash/manual/bash.html#Bash-Variables>
regarding the SECONDS variable, it states that 
" [..] Assignment to this variable resets the count to the value assigned,
and the expanded value becomes the value assigned plus the number
of seconds since the assignment. [..]"

This implies that assigning the variable at time X with value Y would
reset SECONDS to Y. When expanding the variable at time X+Z the
value should be Y+Z. The text also implies whole seconds, i.e. not
milliseconds or other fractions of seconds, are considered. However, it would 
seem as if the
underlying mechanism to update SECONDS (the system clock)
are actually considering fractions of seconds and not whole seconds.
Below is a small programming that shows my point:

#!/bin/bash

while true; do
    SECONDS=0
    sleep 0.5
    if [ "$SECONDS" != "0" ]; then
        printf 'This is unexpected: %s != 0\n' "$SECONDS"
    fi
done

As we sleep less than a full second the expanded value of SECONDS should never 
be greater than 0 but it sometimes is. I guess this is because the assignment 
might occur, say X.7 seconds and the expanded value will then read (X+1).2 
which would be rounded down to (X+1).

This might not be a bug but it is at least misleading when reading the 
documentation and produces unexpected and inconsistent behaviour in some cases.

Version of Bash used to test this behaviour:
GNU bash, version 5.2.32(1)-release (x86_64-pc-linux-gnu)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]