From the Terminal

Timing things in Bash

When building Continuous Integration YAML documents I regularly need to time how long something took.

To do this we can store the current amount of $SECONDS in a variable.

last=$SECONDS

We then define a bash function

sincelast() { duration=$(( SECONDS - last )); echo \'$1\' took $duration seconds;  last=$SECONDS; }

To use this you simply run:

sincelast "pecl install xdebug"

and that will print

'pecl install xdebug' took 16 seconds