How do I get just real time value from 'time' command?

BashScriptingShell

Bash Problem Overview


I would like to make a script that outputs only the real time value from the time command so I can plot the results. For example time command outputs

real	1m0.001s
user	1m0.000s
sys	 0m0.001s

I want to write a script that outputs

60.001 

How do I get just real time value from 'time' command in seconds?

Bash Solutions


Solution 1 - Bash

If you're using the Bash builtin time, set the TIMEFORMAT variable to %R:

$ TIMEFORMAT=%R
$ time sleep 1
1.022

Solution 2 - Bash

time can take an optional --format or -f parameter, but you have to use the full path to the time command, /usr/bin/time

/usr/bin/time -f "%e" sleep 3

3.00

Without the path, it'll use the time command of the shell which just treats all arguments as the command, so you'll get an -f: command not found.

Solution 3 - Bash

If you write \time you enforce not to use the bash built in time. So with \time -f '%e' command you are done.

Solution 4 - Bash

Alternatively, use /usr/bin/time or take a look at the similar question
Using time command in bash script.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew SowdersView Question on Stackoverflow
Solution 1 - BashDennis WilliamsonView Answer on Stackoverflow
Solution 2 - BasheumiroView Answer on Stackoverflow
Solution 3 - BashMartin T.View Answer on Stackoverflow
Solution 4 - BashArcView Answer on Stackoverflow