Hello, this follow-up post goes with my latest short late-night video on YouTube, which you can watch below.
A few clarifications:
1) Outside a bash script, in your session, the $SECOND variable will display the number of seconds elapsed since the corresponding terminal session was opened.
2) If you want to time different sections of the script, rather than measure from its start, you can both:
– reset the variable whenever you want
– store its value at a given moment in another variable.
You can therefore save each value you need in a variable, then use whatever arithmetic operations you like to get the result you want. For example:
echo Partie 1 :
sleep 3
PARTIE1=$SECONDS
echo "Temps écoulé durant la partie 1 : $PARTIE1 secondes"
SECONDS="0"
echo Partie 2 :
sleep 2
PARTIE2=$SECONDS
echo "Temps écoulé durant la partie 2 : $PARTIE2 secondes"
echo "temps total écoulé : $(($PARTIE1+$PARTIE2)) secondes"
This gives:
Partie 1 :
Temps écoulé durant la partie 1 : 3 secondes
Partie 2 :
Temps écoulé durant la partie 2 : 2 secondes
temps total écoulé : 5 secondes
Enjoy!
—
Christophe Casalegno
You can follow me on: Twitter | Facebook | Linkedin | Telegram | Youtube

3 comments