With the following bash script:
#!/bin/bash
let SECONDS=5
until [[ "$SECONDS" -le "1" ]]; do
echo SECONDS $SECONDS
(( SECONDS -= 1 ))
# sleep 1
done
I am seeing strange behavior. Namely, that if I uncomment the sleep 1
the loop continues but the counter does not decrement, I see output like:
$./mvce.sh
SECONDS 5
SECONDS 5
SECONDS 5
SECONDS 5
SECONDS 5
SECONDS 5
SECONDS 5
Removing that line results in the expected:
$./mvce.sh
SECONDS 5
SECONDS 4
SECONDS 3
SECONDS 2
I'm not really sure why this is happening. I can rename the variable to something else and things work as expected so it seems that something about sleep
breaks my SECONDS
variable.
This was surprising, why does calling sleep override the SECONDS
variable within my script?
SECONDS
is reserved variable in shell. That's why you must always use lowercase or mixed case variables in your script and avoid using all uppercase variable names.That gives expected output:
Doumentation: