How do I measure duration in seconds in a shell script?

I wish to find out how long an operation takes in a Linux shell script. How can I do this?


Using the time command, as others have suggested, is a good idea.

Another option is to use the magic built-in variable $SECONDS, which contains the number of seconds since the script started executing. You can say:

START_TIME=$SECONDS
dosomething
ELAPSED_TIME=$(($SECONDS - $START_TIME))

I think this is bash-specific, but since you're on Linux, I assume you're using bash.


Use the time command. time ls /bin.


Try following example:

START_TIME=$SECONDS
# do something
sleep 65

ELAPSED_TIME=$(($SECONDS - $START_TIME))

echo "$(($ELAPSED_TIME/60)) min $(($ELAPSED_TIME%60)) sec"    
#> 1 min 5 sec