I want to do a bash script that measures the launch time of a browser for that I am using an html which gets the time-stamp on-load in milliseconds using JavaScript.

In the shell script just before i call the browser I get the time-stamp with:

date +%s

The problem is that it gets the time-stamp in seconds, and I need it in milliseconds since sometimes when ran a second time the browser starts in under a second and I need to be able to measure that time precisely using milliseconds instead of seconds.

How can I get the time-stamp in milliseconds from a bash script?


date +%s.%N will give you, eg., 1364391019.877418748. The %N is the number of nanoseconds elapsed in the current second. Notice it is 9 digits, and by default date will pad this with zeros if it is less than 100000000. This is actually a problem if we want to do math with the number, because bash treats numbers with a leading zero as octal. This padding can be disabled by using a hyphen in the field spec, so:

echo $((`date +%s`*1000+`date +%-N`/1000000))

would naively give you milliseconds since the epoch.

However, as Stephane Chazelas points out in comment below, that's two different date calls which will yield two slightly different times. If the second has rolled over in between them, the calculation will be an entire second off. So:

echo $(($(date +'%s * 1000 + %-N / 1000000')))
  • I tried it and it works, thanks +1/accept! – Eduard Florinescu Mar 27 '13 at 13:38
  • 6
    Note that in between the time you run the first date and the second date, several million nanoseconds may have passed and it might not even be the same second. Best to do $(($(date +'%s * 1000 + %N / 1000000'))). That still doesn't make much sense, but would be more reliable. – Stéphane Chazelas Mar 27 '13 at 16:24
  • @StephaneChazelas Ouch, yeah that is a pitfall, will edit. Thx. – goldilocks Mar 27 '13 at 16:41
  • 1
    Fixed by turning off zero padding as noted in date(1) – Alois Mahdal Mar 27 '13 at 19:09
  • 2
    %N isn't available in BSD and OS X. – orkoden Jan 29 '15 at 11:20

I would use:

$ date +%s%3N

which returns milliseconds since Unix epoch.


In case anyone is using other shells than bash, ksh93 and zsh have a floating point $SECONDS variable if you do a typeset -F SECONDS which can be handy to measure time with accuracy:

$ typeset -F SECONDS=0
$ do-something
something done
$ echo "$SECONDS seconds have elapsed"
18.3994340000 seconds have elapsed

Recent versions of zsh have a $EPOCHREALTIME special floating point variable in the zsh/datetime module:

$ zmodload zsh/datetime
$ printf '%d\n' $((EPOCHREALTIME*1000))

To avoid calculations to get the milliseconds, command line JavaScript interpreters may help:

bash-4.2$ node -e 'console.log(new Date().getTime())' # node.js

bash-4.2$ js -e 'print(new Date().getTime())'         # SpiderMonkey

bash-4.2$ jsc-3 -e 'print(new Date().getTime())'      # WebKit

bash-4.2$ seed -e 'new Date().getTime()'              # GNOME object bridge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.