Shell – bash script: fixed sleep time is adding up in while loop

shell-scriptsleep

I am new to bash scripting and started out with a few sample scripts.

One is:

#!/bin/bash
SECONDS=5
i=1

while true
do
        echo "`date`: Loop $i"
        i=$(( $i+1 ))
        sleep $SECONDS
done

This results in:

Sunday 10 May  15:08:20 AEST 2020: Loop 1
Sunday 10 May  15:08:25 AEST 2020: Loop 2
Sunday 10 May  15:08:35 AEST 2020: Loop 3
Sunday 10 May  15:08:55 AEST 2020: Loop 4

… and is not what I expected or wanted the script to do.

Why would the seconds double, every time it runs through the loop

bash --version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

Best Answer

Because SECONDS is an internal variable in bash. It stores the amount of time that the shell has been running.

From man bash:

SECONDS
Each time this parameter is referenced, the number of seconds since shell invocation is returned. 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.

So, your script works like this:

  1. On first use it accepts the value of 5 as input, the initial value is 5.
  2. Then there is an sleep of 5 seconds, when that sleep returns, the value of SECONDS will be the 5 seconds of the initial value + 5 seconds of the sleep = 10 seconds
  3. Then there is an sleep of 10 seconds (the present value of SECONDS) plus the previous accumulated time of 10 seconds will give 20.
  4. Repeat an sleep for 20 seconds plus the previous value of 20 is 40.
  5. etc.
Related Question