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
There is a reason for the convention for not using upper case variable names. Bash and env variables are all upper cased, so this is a classic example.
See: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html