Redirect value from time function to file

Asked

Viewed 101 times

1

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

Commando:

time ./main

Exit:

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

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

Commando:

time ./main >> teste.txt

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

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.

(griffin mine)

That is, the time does not send the exit to the stdout, and yes to the stderr, so the direction 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 the output from time and not that of main. In the first case, we are grouping the execution into a codeblock (hence 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).

  • The answer raised another question, shell is synonymous with subshell?

  • 1

    @pmargreff is a new shell instance executed by a shell. It’s the same thing, they just came up with a different name because it’s a shell that called each other.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.