help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Duration between two time stamps


From: Seth David Schoen
Subject: Re: Duration between two time stamps
Date: Wed, 18 Aug 2021 19:01:43 -0700

hancooper via writes:

> How can I use two time stamps and compute the elapsed time?
> I have a search command that I want to also compute the execution time

Well, bash's built-in arithmetic only acts on integers, so if the time
stamps have a decimal part or you want precision less than one second,
you can't directly do it natively in pure bash.

If you want precision only accurate to a whole second, you can use
$(($B-$A)), like

$ B=1245
$ A=1240
$ echo $(($B-$A))
5

But this won't work if there is a decimal point in either number.  In
that case, you might want to pipe into bc, like

$ B=1245.12983
$ A=1240.91289
$ echo "$B-$A" | bc
4.21694

This is slower because it creates a subprocess to perform the arithmetic
calculation.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]