I want to run a process in bash and save in an env variable the number of seconds it took to run. How would I do such a thing?
Using $SECONDS
wasn't working for me in a script run by cron
(though it worked when I ran it directly; I used cron
for the first time today, so there could be some user error too). @Farzy's answer using TIMEFORMAT
worked but I didn't want to redirect the output from the timed command. Here's an alternative to $SECONDS
if you're in a similar situation:
start=$(date +%s)
your_command
seconds=$(($(date +%s) - $start))