Measuring time within a script

You could just use time:

time (
Line 1
Line 2
..
Line N
)

I think the output of time is human readable as is, but if your script is going to measure in days, etc., then check out man time for formatting options for the output.


You can use date util:

#!/bin/bash

start_measuring_time() {
  read s1 s2 < <(date +'%s %N')
}

stop_measuring_time() {
  read e1 e2 < <(date +'%s %N')
}

show_elapsed_time() {
  echo "$((e1-s1)) seconds, $((e2-s2)) nanoseconds"
}

start_measuring_time
sleep 2
stop_measuring_time
show_elapsed_time