You've referenced the
bash internal variable
SECONDS (which outputs the number of seconds elapsed since the current instance of shell is invoked) and saved the value as another variable
time_spent. Now, after that every time you check the value of variable
time_spent, you would get the same value -- the saved one, at the time of expansion of
To dynamically get
SECONDS, you should reference
$SECONDS directly rather than using an intermediate variable:
echo "Time: $SECONDS"
If you insist on using an intermediate variable, make sure to do the expansion of
$SECONDS every time.
Regarding the value of
0, you can easily reproduce this:
% bash -c 'echo $SECONDS'
The point is: when you're calculating the value, it's not a second yet, so the value is being