10

I want to save the result of the $SECONDS command into a variable, to print time in format HH:MM:SS.

This is what I've tried so far. I tried to print it in different ways and was always getting the result as 0:

time_spent="$SECONDS"
echo "Time: $time_spent"

I want to print time spent in the shell when I close my console. I created a script that runs every time the console was closed.

1
  • How did you configure your script to run after the console is ended? Do you mean gnome-terminal or Alt+F1 console? There might be other options available... Apr 27, 2018 at 21:57

5 Answers 5

13

To get $SECONDS into HH:MM:SS format you will need to do some (integer) math:

secs=$SECONDS
hrs=$(( secs/3600 )); mins=$(( (secs-hrs*3600)/60 )); secs=$(( secs-hrs*3600-mins*60 ))

printf 'Time spent: %02d:%02d:%02d\n' $hrs $mins $secs
Time spent: 431:48:03
4
  • 18
    I think it would be better to save $SECONDS as a variable first -- to keep consistency (not sure how much of fraction of a second would be left for doing the last two calculations).
    – heemayl
    Apr 27, 2018 at 19:30
  • @steeldriver do you know if there is a milliseconds version of the same solution ? thx
    – BuriB
    Jun 24, 2021 at 14:12
  • @heemayl good point, done - sorry I only just noticed your comment as the result of the recent one Jun 24, 2021 at 14:27
  • @BuriB no I don't - sorry Jun 24, 2021 at 14:29
10

If you're sure that $SECONDS will be less than 1 day (i.e. 86400 seconds), then GNU core-utils date does a pretty good job of the required formatting:

$ date -ud "@$SECONDS" "+Time elapsed: %H:%M:%S"
Time elapsed: 00:32:05
$
7

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 SECONDS.

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 SECONDS being 0, you can easily reproduce this:

% bash -c 'echo $SECONDS'
0

The point is: when you're calculating the value, it's not a second yet, so the value is being 0, correctly.

2
  • this is the whole script, i think it's because i execute a shell script and at this moment the SECONDS variable value is 0. WHat might be a solution for this kind of work. I' need the time spend in shell output when i close my console, so i created shell script which run everytime i close console
    – Andrew
    Apr 27, 2018 at 19:22
  • @Andrew Check my edits.
    – heemayl
    Apr 27, 2018 at 19:23
5

Here is a quick one that gives hours minutes and seconds since the shell has been opened:

~$ cat how_long_open 
#!/bin/bash

time=$SECONDS
printf '%dh:%dm:%ds\n' $(($time/3600)) $(($time%3600/60)) $(($time%60))

Since scripts run in a subshell, the best way to get output is to source the script instead of calling it.

Examples:

Without sourcing the script

~$ ./how_long_open 
0h:0m:0s

With sourcing the script

~$ source ./how_long_open 
1h:24m:40s

Hope this helps!

1

Another way to use printf:

SECONDS=0
# do something
TZ=UTC printf '%(%H:%M:%S)T\n' $SECONDS

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .