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.


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

$ nano
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 “” 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 “” in this way: 

$ ./

The “” 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:

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

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

$ ./

The execution time of the “” 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.


The “” script is opened in the “nano” editor:

$ nano
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 “” script and check its execution time:

$ ./

The “” 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.


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

$ nano
TIMEFORMAT= ‘The execution time in %R seconds’time {sleep 1sleep 2}

The “” 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:

$ ./

The “” was executed in “3.048” seconds.


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.