Get Execution Time for a Shell Script

If you need to know the execution time for a bash script, you can place it inside the script below. The the total run time will be printed to the screen after the script finishes.

#!/bin/bash
res1=$(date +%s.%N)

<your script here>

res2=$(date +%s.%N)
dt=$(echo "$res2 - $res1" | bc)
dd=$(echo "$dt/86400" | bc)
dt2=$(echo "$dt-86400*$dd" | bc)
dh=$(echo "$dt2/3600" | bc)
dt3=$(echo "$dt2-3600*$dh" | bc)
dm=$(echo "$dt3/60" | bc)
ds=$(echo "$dt3-60*$dm" | bc)
printf "Total runtime: %d:%02d:%02d:%02.4f\n" $dd $dh $dm $ds

Shamelessly copied from a post on unix.stackexchange.com by jwchew on August 30, 2013 .