Redirect function time value to file

1

I'm trying to use the time function on the terminal. When I try to use only to display always I have the expected return.

Command:

time ./main

Output:

real    0m0.119s
user    0m0.113s
sys     0m0.006s

But when I try to redirect this to a pipe file like the example below, the file remains blank and the output is printed on the screen.

Command:

time ./main >> teste.txt

Does anyone know what might be wrong and / or how do I get the expected result?

    
asked by anonymous 03.05.2016 / 00:59

1 answer

3

According to the manual of time

  

The time command runs the specified program command with the given arguments. When command finishes, time writes a message to standard error giving timing statistics about this program run.

(emphasis mine)

That is, time does not send the output to stdout , but to stderr , so the targeting has to be 2>> :

{ time /.main; } 2>> teste.txt

or with subshell:

( time ./main ) 2>> teste.txt

Note the brackets and / or parentheses to redirect output from time and not main . In the first case, we are grouping the execution in a codeblock (so the ; at the end of the command). In the second, we are calling a subshell, to do the grouping (in this case it is a waste of resources).

    
03.05.2016 / 01:02