How to Get the Execution Time of a Bash Script Effectively?

The “Bash” script is a plain text file that contains multiple commands to perform different tasks on the system. The bash script executes line by line and returns the output instantly if there is no syntax or logical error in the commands. 

In the case of large scripts i.e thousand lines of code/commands, the bash script takes a long time to for execution. The total time in which the bash script executes is called its execution time. It can be checked effectively by subtracting the start and ending times of a bash script.

This post pens down all possible methods to get the execution time of a bash script effectively.

Method 1: Using the “date” Command

The “date” command is the built-command line tool that sets and displays the current date and time of the system. It can also be used in a bash script for getting its execution time.

Example:

In this example, create and open the bash script named “Time.sh” using the nano text editor in Ubuntu:

$ nano Time.sh
#!/bin/bash
start=`date +%s`sleep 2sleep 4end=`date +%s`echo "Execution Time is: $(($end-$start)) seconds"

The above code block defines the following parameters:

  • The “#! /bin/bash” first line is the “Bash Shebang” that executes the script in “bash” shell.
  • The “start” is the variable that contains the “date” command with an addition of “%s” i.e seconds.
  • The two “sleep” commands execute the “Time.sh” bash script after 2 and 4 seconds.
  • The “end” variable tells the seconds in which the script will end.
  • Finally, the “echo” command prints the result of execution time $(($end-$start)) along with the specified statement.

Press “Ctrl+S” to save and “Ctrl+X” to exit the script.

Execute the script by passing the bash script name “Time.sh” in this way: 

$ ./Time.sh

The “Time.sh” script has been executed after six seconds, that’s why its execution time is “6 seconds”.

Execution Time in “nanoseconds”

The user can also get the execution time in nanoseconds by using the character “%N” with the “date” command in this way:

#!/bin/bash
start=`date +%s%N`
sleep 2
sleep 4
end=`date +%s%N`
echo "Execution Time is: $(($end-$start)) seconds"

At this time the “Time.sh” script execution time will be displayed more precisely i.e in “nanoseconds”:

$ ./Time.sh

The execution time of the “Time.sh” bash script is “6042959501” nanoseconds.

Method 2: Using the “$SECONDS” Bash Internal Variable

The “$SECOND” is an internal bash variable that returns the number of seconds in which the bash shell script completes its execution. For this purpose, declare a variable  “$SECOND” having an integer value from where the execution time will start.

Example:

The “Code.sh” script is opened in the “nano” editor:

$ nano Code.sh
#!/bin/bash
SECONDS=0sleep 3sleep 6echo "Execution Time is : $SECONDS seconds"

The description of code is written here:

  • In the above code, the “SECONDS” variable value is set “0” from where the count is started.
  • The “echo” command displays the “$SECOND” variable value.

Run the “Code.sh” script and check its execution time:

$ ./Code.sh

The “Code.sh” has been executed in “9” seconds.

Method 3: Using the Bash “TIMEFORMAT” 

The “TIMEFORMAT” is the parameter that specifies the time value as a string with the time reserved word such as “%R” for the execution time of the bash script in seconds.

Example:

Create/open a “Program.sh” script in the “nano” text editor:

$ nano Program.sh
#!/bin/bash
TIMEFORMAT= ‘The execution time in %R seconds’time {sleep 1sleep 2}

The “Program.sh” script is defined below:

  • The “TIMEFORMAT” is the string that displays/prints after the successful execution of the “time{}” code block.
  • The “time” block wraps the two sleep commands having values 1 and 2.

Execute the script:

$ ./Program.sh

The “Program.sh” was executed in “3.048” seconds.

Conclusion

In Linux, use the “date”, “$SECONDS” internal bash variable, and the “TIMEFORMAT” string to get the execution time of the bash script. All these utilities return the execution time in an effective manner. Apart from seconds, the “date” command can also print the execution time in “nanoseconds” using the “%N” character.

This post has provided all possible ways to get the execution time of the bah script effectively.