How do I use $SECONDS inside a bash script?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
4
down vote
favorite
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.
bash scripts
add a comment |Â
up vote
4
down vote
favorite
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.
bash scripts
How did you configure your script to run after the console is ended? Do you meangnome-terminal
or Alt+F1 console? There might be other options available...
â WinEunuuchs2Unix
Apr 27 at 21:57
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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.
bash scripts
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.
bash scripts
edited Apr 28 at 15:20
Eliah Kagan
79.4k20221359
79.4k20221359
asked Apr 27 at 19:08
![](https://lh5.googleusercontent.com/-SQeHsmhEPgU/AAAAAAAAAAI/AAAAAAAAARk/gep_9Rj3uh0/photo.jpg?sz=32)
![](https://lh5.googleusercontent.com/-SQeHsmhEPgU/AAAAAAAAAAI/AAAAAAAAARk/gep_9Rj3uh0/photo.jpg?sz=32)
Andrew
1824
1824
How did you configure your script to run after the console is ended? Do you meangnome-terminal
or Alt+F1 console? There might be other options available...
â WinEunuuchs2Unix
Apr 27 at 21:57
add a comment |Â
How did you configure your script to run after the console is ended? Do you meangnome-terminal
or Alt+F1 console? There might be other options available...
â WinEunuuchs2Unix
Apr 27 at 21:57
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...â WinEunuuchs2Unix
Apr 27 at 21:57
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...â WinEunuuchs2Unix
Apr 27 at 21:57
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
7
down vote
accepted
To get $SECONDS
into HH:MM:SS format you will need to do some (integer) math:
hrs=$(( SECONDS/3600 )); mins=$(( (SECONDS-hrs*3600)/60)); secs=$(( SECONDS-hrs*3600-mins*60 ))
printf 'Time spent: %02d:%02d:%02dn' $hrs $mins $secs
Time spent: 431:48:03
14
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 at 19:30
add a comment |Â
up vote
5
down vote
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.
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 at 19:22
@Andrew Check my edits.
â heemayl
Apr 27 at 19:23
add a comment |Â
up vote
4
down vote
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:%dsn' $(($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!
add a comment |Â
up vote
4
down vote
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
$
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
To get $SECONDS
into HH:MM:SS format you will need to do some (integer) math:
hrs=$(( SECONDS/3600 )); mins=$(( (SECONDS-hrs*3600)/60)); secs=$(( SECONDS-hrs*3600-mins*60 ))
printf 'Time spent: %02d:%02d:%02dn' $hrs $mins $secs
Time spent: 431:48:03
14
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 at 19:30
add a comment |Â
up vote
7
down vote
accepted
To get $SECONDS
into HH:MM:SS format you will need to do some (integer) math:
hrs=$(( SECONDS/3600 )); mins=$(( (SECONDS-hrs*3600)/60)); secs=$(( SECONDS-hrs*3600-mins*60 ))
printf 'Time spent: %02d:%02d:%02dn' $hrs $mins $secs
Time spent: 431:48:03
14
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 at 19:30
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
To get $SECONDS
into HH:MM:SS format you will need to do some (integer) math:
hrs=$(( SECONDS/3600 )); mins=$(( (SECONDS-hrs*3600)/60)); secs=$(( SECONDS-hrs*3600-mins*60 ))
printf 'Time spent: %02d:%02d:%02dn' $hrs $mins $secs
Time spent: 431:48:03
To get $SECONDS
into HH:MM:SS format you will need to do some (integer) math:
hrs=$(( SECONDS/3600 )); mins=$(( (SECONDS-hrs*3600)/60)); secs=$(( SECONDS-hrs*3600-mins*60 ))
printf 'Time spent: %02d:%02d:%02dn' $hrs $mins $secs
Time spent: 431:48:03
answered Apr 27 at 19:26
steeldriver
62.5k1196164
62.5k1196164
14
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 at 19:30
add a comment |Â
14
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 at 19:30
14
14
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 at 19:30
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 at 19:30
add a comment |Â
up vote
5
down vote
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.
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 at 19:22
@Andrew Check my edits.
â heemayl
Apr 27 at 19:23
add a comment |Â
up vote
5
down vote
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.
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 at 19:22
@Andrew Check my edits.
â heemayl
Apr 27 at 19:23
add a comment |Â
up vote
5
down vote
up vote
5
down vote
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.
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.
edited Apr 27 at 19:23
answered Apr 27 at 19:16
![](https://i.stack.imgur.com/8mGqv.gif?s=32&g=1)
![](https://i.stack.imgur.com/8mGqv.gif?s=32&g=1)
heemayl
63.9k8126202
63.9k8126202
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 at 19:22
@Andrew Check my edits.
â heemayl
Apr 27 at 19:23
add a comment |Â
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 at 19:22
@Andrew Check my edits.
â heemayl
Apr 27 at 19:23
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 at 19:22
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 at 19:22
@Andrew Check my edits.
â heemayl
Apr 27 at 19:23
@Andrew Check my edits.
â heemayl
Apr 27 at 19:23
add a comment |Â
up vote
4
down vote
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:%dsn' $(($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!
add a comment |Â
up vote
4
down vote
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:%dsn' $(($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!
add a comment |Â
up vote
4
down vote
up vote
4
down vote
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:%dsn' $(($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!
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:%dsn' $(($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!
answered Apr 27 at 20:59
![](https://i.stack.imgur.com/1hPwN.jpg?s=32&g=1)
![](https://i.stack.imgur.com/1hPwN.jpg?s=32&g=1)
Terrance
17.2k23784
17.2k23784
add a comment |Â
add a comment |Â
up vote
4
down vote
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
$
add a comment |Â
up vote
4
down vote
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
$
add a comment |Â
up vote
4
down vote
up vote
4
down vote
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
$
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
$
answered Apr 27 at 21:22
![](https://i.stack.imgur.com/NcQBc.png?s=32&g=1)
![](https://i.stack.imgur.com/NcQBc.png?s=32&g=1)
Digital Trauma
1,495517
1,495517
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1028924%2fhow-do-i-use-seconds-inside-a-bash-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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...â WinEunuuchs2Unix
Apr 27 at 21:57