[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Can Bash do simple math?
From: |
Bash-help |
Subject: |
Re: Can Bash do simple math? |
Date: |
Tue, 06 Aug 2024 16:08:31 +0200 |
User-agent: |
K-9 Mail for Android |
On Tue, Aug 06, 2024 at 03:11:26PM +0200, Emanuele Torre wrote:
>
> No, that is docuemented:
>
> SECONDS
>Each time this parameter is referenced, it expands to the number of
>seconds since shell invocation. If a value is assigned to SECONDS,
>the value returned upon subsequent references is the number of
>seconds since the assignment plus the value assigned. The number
>of seconds at shell invocation and the current time are always
>determined by querying the system clock. If SECONDS is unset, it
>loses its special properties, even if it is subsequently reset.
>
It may be documented but I consider it misleading as the text
implies whole seconds to be considered when it seems that the underlying
mechanism for getting the time from the system clock actually
has millisecond (or smaller) precision. This makes the variable produce
unexpected output in some cases, as my failing script shows.
For example: assigning SECONDS at time X and reading SECONDS Y
seconds later should always produce a time X+Y but if Y=0,5 second
the expanded value depends on what half-second the assignment took
place. Or so it seems!
Below is a simple script 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 never wait more than half a second the value of SECONDS should never be
greater than 0 but it sometimes is. :)
Re: Can Bash do simple math?, alex xmb sw ratchev, 2024/08/06
- Re: Can Bash do simple math?, alex xmb sw ratchev, 2024/08/06
- Re: Can Bash do simple math?, Greg Wooledge, 2024/08/06
- Re: Can Bash do simple math?, alex xmb sw ratchev, 2024/08/06
- Re: Can Bash do simple math?, alex xmb sw ratchev, 2024/08/06
- Re: Can Bash do simple math?, alex xmb sw ratchev, 2024/08/06
- Re: Can Bash do simple math?, Bash-help, 2024/08/06
- Re: Can Bash do simple math?, alex xmb sw ratchev, 2024/08/06
Re: Can Bash do simple math?, Greg Wooledge, 2024/08/06
Re: Can Bash do simple math?, alex xmb sw ratchev, 2024/08/06