I wish to find out how long an operation takes in a Linux shell script. How can I do this?
Many of the answers mention $SECONDS, but that variable is actually even better than they realize:
Assignment to this variable resets the count to the value assigned, and the expanded value becomes the value assigned plus the number of seconds since the assignment.
This means you can simply query this variable directly at the end of your script to print the elapsed time:
#!/usr/bin/env bash
# Do stuff...
echo "Script finished in $SECONDS seconds."
You can also time smaller sections like so:
#!/usr/bin/env bash
# Do stuff
SECONDS=0
# Do timed stuff...
echo "Timed stuff finished in $SECONDS seconds."