8 Jul 2026 12:00

HPR4678: High Resolution Elapsed Time in Shell Scripts

This show has been flagged as Clean by the host.

01 Introduction

In this episode I will describe how to calculate elapsed time in bash or other shell scripts.

While this may sound like a very simple and basic thing to do, there is a slightly more complex aspect to it if you wish to calculate elapsed time to a higher resolution than one second.

02

There are many reasons for calculating elapsed time in a shell script.

For example you may wish to simply report how long an operation took to run.

Another reason may be that you are trying to speed up a script and need to calculate benchmark data to see how different alternative methods perform.

03

What may seem like a simple task gets a bit more complicated if you want to do it for multiple different operating systems even if they are all unix related, as we shall see.

--------------------

04 Operating Systems Tested

For the purposes of this episode, I ran tests on the current version of the following operating systems.

Alma

Alpine

Debian

FreeBSD

OpenBSD

RaspberryPi

OpenSuse

Ubuntu 2604

Alma is a close copy of Red Hat that we can take as representing Red Hat style distros.

--------------------

05 Simple Low Resolution Timing

I will start with the simple and obvious method before describing the less obvious ones.

This uses the date command to get the current time in seconds since the unix epoch.

This is simply

date '+%s'

06

Save this to a variable using whatever method you prefer.

For example.

starttime=$(date '+%s')

07

Next, do whatever operations it is you wish to time.

Use the date command to get the current time again.

endtime=$(date '+%s')

08

Now simply subtract the start time from the end time using shell arithmetic.

This should be very obvious and basic.

--------------------

09 Higher Resolution Timing

However, suppose we wish to measure time to greater than one second of precision.

We need to do two things.

The first is to obtain the current time at a higher degree of precision.

The second is to conduct the calculations to a higher degree of precision.

10

Unfortunately, the standard time precision for POSIX shells seems to be 1 second.

Some shells offer a higher precision, but others do not.

Furthermore, standard shell arithmatic uses integer, which limits calculations to 1 second of precision.

--------------------

11 Bash High Resolution Shell Variable

Fortunately, bash is one that does offer a high precision date.

If you are using bash 5.0 or newer, there is a shell variable called EPOCHREALTIME which offers time since the the unix epoch (that is, since the first of January 1970, at 00:00:00 UTC) in seconds to 6 decimals of precision.

12

Example

echo $EPOCHREALTIME

1779634800.184926

13

This is related to the similar bash variable known as EPOCHSECONDS which gives the number of seconds since the unix epoch.

14

Example

echo $EPOCHSECONDS

1779634800

15

So if you are using bash, measuring time is very simple.

--------------------

16 But is it Really Bash?

Is your script however actually using bash?

Debian and derivatives actually have two shells.

The first, the interactive shell is bash.

The second, the non-interactive shell is dash, which stands for "debian almquist shell".

17

If you open a terminal, you get bash.</


Отзывы


Podcastly – лучшая платформа для любителей подкастов. Более 10 миллионов аудио контента доступных на Android/iOS/Web/Desktop и Telegram.