From the bash
manual:
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.
If SECONDS
is unset, it loses its special properties, even if it
is subsequently reset.
So whenever you want to time a piece of code with a timer that counts seconds, you may do
SECONDS=0
# code goes here
t=$SECONDS
To print days and minutes from the $t
value:
printf 'Time taken: %d days, %d minutes\n' "$(( t/86400 ))" "$(( t/60 - 1440*(t/86400) ))"
$SECONDS
variable that counts seconds since the script started? – Kusalananda May 26 '20 at 13:49It works with the solution you provided below.
– Keshav Boodhun May 29 '20 at 04:57