I print the start and end time using date +"%T", which results in something like:
10:33:56
10:36:10
How could I calculate and print the difference between these two?
I would like to get something like:
2m 14s
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Bash has a handy
SECONDSbuiltin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value.Thus, you can just set
SECONDSto 0 before starting the timed event, simply readSECONDSafter the event, and do the time arithmetic before displaying.As this solution doesn’t depend on
date +%s(which is a GNU extension), it’s portable to all systems supported by Bash.Documentation: