Buy this tutorial as a PDF for only $5
9 May 2016
Formatting and Displaying Time in Hours, Minutes and Seconds
Sometimes a script will take a varying amount of time to run, and you want it to report how long it took. Sure, you can say when it started and finished, but that's not particularly helpful. The GNU Date command has the "date +%s
" option, which gives you the number of seconds since 1st January, 1970. By noting that value before and after running the script, you can get a nice simple integer telling you how many seconds the script took to run.
So you can display a message saying "That took 11723 seconds to run", for example. It would be nicer to say "That took 3 hours, 15 minutes and 23 seconds to run", though.
This function uses Bash's simple built-in integer mathematical capabilities to display a duration (in seconds) in a more human-readable form. For completeness, it actually offers two different formats; by default, it displays "03h15m23s", but if you call it with the word "long" as the second argument, it will say "3 hours, 15 minutes and 23 seconds".
These little touches make a script just that bit more friendly and useful.
#!/bin/bash # https://www.shellscript.sh/tips/hms/ # Example of Hours, Minutes, Seconds. # Look for the "*** To avoid having to wait" comments # for a quicker way of playing with this! # Make a note of the start time (in seconds since 1970) STARTTIME=`date +%s` echo "`date`: Starting the script." # Define hms() function hms() { # Convert Seconds to Hours, Minutes, Seconds # Optional second argument of "long" makes it display # the longer format, otherwise short format. local SECONDS H M S MM H_TAG M_TAG S_TAG SECONDS=${1:-0} let S=${SECONDS}%60 let MM=${SECONDS}/60 # Total number of minutes let M=${MM}%60 let H=${MM}/60 if [ "$2" == "long" ]; then # Display "1 hour, 2 minutes and 3 seconds" format # Using the x_TAG variables makes this easier to translate; simply appending # "s" to the word is not easy to translate into other languages. [ "$H" -eq "1" ] && H_TAG="hour" || H_TAG="hours" [ "$M" -eq "1" ] && M_TAG="minute" || M_TAG="minutes" [ "$S" -eq "1" ] && S_TAG="second" || S_TAG="seconds" [ "$H" -gt "0" ] && printf "%d %s " $H "${H_TAG}," [ "$SECONDS" -ge "60" ] && printf "%d %s " $M "${M_TAG} and" printf "%d %s\n" $S "${S_TAG}" else # Display "01h02m03s" format [ "$H" -gt "0" ] && printf "%02d%s" $H "h" [ "$M" -gt "0" ] && printf "%02d%s" $M "m" printf "%02d%s\n" $S "s" fi } ############################################################ # Main script - could be anything, but takes a # non-predictable amount of time. # Sleep for 123 seconds (2m 3s) unless some other value # is passed to the script. # *** To avoid having to wait, comment out the following line: sleep ${1:-123} ############################################################ # At end of script, report how long it took. # Make a note of the end time (in seconds since 1970) echo "`date`: The script has finished." ENDTIME=`date +%s` # Calculate the difference - that's how long the script took to run let DURATION=${ENDTIME}-${STARTTIME} # *** To avoid having to wait, uncomment the following line: #DURATION=$1 HOWLONG=`hms $DURATION` echo "That took ${HOWLONG} to run (short format)" HOWLONG=`hms $DURATION long` echo "That took ${HOWLONG} to run (long format)"Download the hms.sh script
An example of running the script, with a sleep
of 123 seconds (two minutes and three seconds), looks like this:

An example of running the script, with a sleep
of 11723 seconds (three hours, fifteen minutes and twenty-three seconds), looks like this:

Appreciate this site? Please consider making a donation:
Books and eBooks
My Shell Scripting books, available in Paperback and eBook formats.
![]() Shell Scripting Tutorial is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and to keep by your desk as an ever-present companion. | ![]() Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half explains the features of the shell; the second half has real-world shell scripts, organised by topic, with detailed discussion of each script. |
Contact
You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!