Display execution time of shell commands in seconds

I first tried using the time utility but failed on converting the result into seconds:

$ { time ls -l >/dev/null; } 2>&1 | grep real | sed -e 's/real//g' | sed -e 's/ //g'
	0m0.006s

Searching the web I found a StackOverflow (Display execution time of shell command with better accuracy) post pointing me to a solution using the bash variable SECONDS

From the Bash Reference Manual:

SECONDS This variable expands to the number of seconds since the shell was started. 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.

Example:

$ SECONDS=0; sleep 5; echo "I slept for $SECONDS seconds"

I slept for 5 seconds

Leave a comment