Storing output of "time" command to a variable

Hi all,

I am new to Linux/shell scripting having moderate knowledge.

In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save the output of time command in a variable. The format of the command is like "time ls -R /opt"

Going further, the o/p of 'time' command is :(Ex:)
real 0m0.003s
user 0m0.004s
sys 0m0.000s
Here, in my script, I would like to use only middle line "user 0m0.004s" saved to the variable but unable to find out the way.

Can someone help me out with this issue. Thanks a lot.

not sure what are you asking for? but anyways try this..

variable=`command`

Hi Tuxidow.

Thanks for the response.
I had tried the format that u posted but didn't work in case of time command. Command, the result of which I would like to save in a variable is : "time ls -R /opt"

Hope this may clear my question more.

Which shell are you using?
This should work with bash:

_utime="$( TIMEFORMAT='%lU';time ( ls ) 2>&1 1>/dev/null )"

Example:

$ _utime="$( TIMEFORMAT='%lU';time ( ls ) 2>&1 1>/dev/null )"
$ echo "$_utime"
0m0.015s

Note that, as per your request, the output will include only the time spent in user mode!

radoulov makes a valid point. The output from most versions of "time" goes to STDERR not STDOUT.

If you haven't got bash:

utime=`time ls -R 2>&1 | grep \^user`

echo $utime
user 0.0

Thanks a lot radoulov & methyl for your replies.

The soloution posted by radoulov seems to be working as desired for my script and I'll continue with that.
Thanks again. :slight_smile:

---------- Post updated 01-23-11 at 07:06 AM ---------- Previous update was 01-22-11 at 02:38 PM ----------

Going further with my post earlier, I am little confused about the output of time command. I mean I can't understand the difference between "real time", "user time" and "sys time" shown by "time" command. Can someone please explain the difference between these outcomes?

I need the execution time of commands like " ls -R /<PATH>" that means, the time taken to display all the files under that PATH recursively.

I have gone through some google searches and found that the required time should be "user time" & therefore, I am using "user time" displayed by "time" command for calculating the total time taken ny "ls -R /<PATH>" command. Please let me know if I am doing right ?? Thanks again.

It's hard to improve on the explanation in "man time".

If you just want to know how long a process took to run, use "real time".
If you want to know how much CPU time the process used while in "user mode" use "user".
If you want to know how much CPU time the process used while in "kernel mode", use "sys".

One facinating aspect of unix is that the statistics from the "time" command may not be the same for the second and subsequent invocations of the same command. A well-tuned system will cache data blocks, directory blocks and program pages as it continuously re-tunes itself.
The difference is most evident after a cold start of the Operating System.

Hi radoulov,

In my bash shell script, I am using the format posted by you i.e.:

_utime="$( TIMEFORMAT='%lU';time ( ls ) 2>&1 1>/dev/null )" 

This displays the user time taken by the ls command. If the "real time" and "system time" are required, in such a case, what options can be used in above format ?
Also, it would be of great help if you could explain about the "l" option used with '%lU'
in above command format.?
Thanks.

Sure,
it's documented in the manual pages, try:

man bash | less -p'^ *TIMEFORMAT'

thanks a lot radoulov.