Saturday, August 18, 2012
3:00 AM

Bash Script: Calculate the total time taken by script execution

There is a bash predefined parameter SECONDS

Each  time  this  parameter  is referenced, the number of seconds since shell invocation is returned.  If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned.  If SECONDS is unset, it loses its special properties, even if it is subsequently reset.

Below is simple bash script which demonstrate the usage of SECOND parameter

Source: cat time_taken.sh
#!/bin/bash

for i in {1..10}; do
        sleep 1
        echo $i
done

echo "Total time taken by this script: $SECONDS"

Output: ./time_taken.sh
1
2
3
4
5
6
7
8
9
10
Total time taken by this script: 10


0 comments:

Post a Comment