
Suppose you wanted to determine the length of time it took to complete the execution of a bash script. Rather than sitting in front of the computer and estimating the time elapsed by watching the script execute with a stopwatch, it can be done with a few lines of code.
Near the top of the script add the following variable which defines the start time.
res1=$(date +%s.%N)
At the bottom of the script add the following variable which defines the end time.
res2=$(date +%s.%N)
printf "Elapsed: %.2F\n" $(echo "$res2 - $res1"|bc )
An example script may look like this.
#!/bin/bash
# timer-test.sh
#
# Timer Start
res1=$(date +%s.%N)
.
.
# The script
.
.
# Timer Stop
res2=$(date +%s.%N)
printf "Elapsed: %.2F\n" $(echo "$res2 - $res1"|bc )