Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

variable in bash script automatically changes after sleep is added

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading