Home > Mobile >  variable (named as SECONDS) in bash script automatically changes after sleep is added
variable (named as SECONDS) in bash script automatically changes after sleep is added

Time:10-31

In my bash script, I tried to assign the argument to a variable and use that variable later, but after I introduced line of sleep, it is found that variable value is changed automatically by adding the number of seconds of that sleep operation. The code is like this (file name chk2.sh):

#!/bin/bash

SECONDS=$1

echo "seconds: $1"
echo "seconds: $SECONDS"
sleep 5;
echo "seconds: $SECONDS"

The output is this:

./chk2.sh 4
seconds: 4
seconds: 4
seconds: 9

I expected the variable $SECONDS is not changing and can be used later. However, the value is actually changed after using sleep.

CodePudding user response:

expected the variable $SECONDS is not changing and can be used later. However, the value is actually changed after using sleep.

declare -p SECONDS

has a value, check the bash manual.

PAGER='less  /^[[:blank:]]*seconds' man bash

  • Related