Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the elapsed time in milliseconds in a bash script?

Tags:

bash

shell

time

I tried using t1=$(date +%s%N) to get the time in nanoseconds, but I kept in getting this error:

./script.sh: line 10: 1292460931N: value too great for base (error token is "1292460931N")

I looked up online and it seems that you can use the "time" command, however I can't find a good example of using the time command. Any help would be appreciated :)

like image 224
Tony Stark Avatar asked Dec 16 '10 01:12

Tony Stark


People also ask

How do I get milliseconds in Linux?

date +"%T. %6N" returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds. date +"%T. %3N" returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.

How do you wait 10 seconds in Bash script?

It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.


1 Answers

The date command you're using doesn't support %N so your output is literally 1292460931N. I tried it on Linux and it worked, but on FreeBSD I see the results you got. Run that date command in a shell and see what comes out. Is it possible you're using busybox? Its cut-down date command also omits %N but the version I just tried gave me 1292463535%N.

like image 53
Ben Jackson Avatar answered Oct 24 '22 12:10

Ben Jackson