2
[fakename]$ { time true; } 2> test.log 
[fakename]$ cat test.log

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

It seems that time is writing to standard error. I've tried redirecting other file descriptors; that always results in the time being printed.

I am led to the conclusion that time writes to stderr. Why is this the case?

4
  • 3
    Because you might want to use the standard output of the command being timed. If it wrote to standard output it would mess up the output of the command.
    – AlexP
    Commented Aug 1, 2018 at 21:42
  • Not to be cheeky, because that is the way it is documented? Can you make a case why it should not be to stderr?
    – dawg
    Commented Aug 1, 2018 at 21:42
  • @dawg Oh. I did not know of that documentation. I was looking at help time, (well, I am also using the builtin one, not time(1)) which didn't provide much helpful information. Thank you. Commented Aug 1, 2018 at 21:51
  • @AlexP Ahh. Makes sense. Thanks. Commented Aug 1, 2018 at 21:52

1 Answer 1

2

Because writing to stdout would make it impossible to time pipes. The time output would vanish in the pipe and might cause errors further down the pipe due to the unexpected input.

5
  • I don’t believe your remark about vanishing is correct. Even if time wrote to stdout, it would write to the stdout at the end of the pipe. I mean, it writes to the stderr at the end of the pipe. As time is a builtin, I believe it operates on different rules. Commented Aug 2, 2018 at 0:12
  • @extremeaxe5 Two "different" stdouts? How would you redirect the timing result to a file then?
    – Jens
    Commented Aug 2, 2018 at 7:06
  • You would redirect it as normal, applying the redirection to the end of the pipe. All I’m saying is that it won’t get “eaten up” in the pipe, as you suggest. Commented Aug 2, 2018 at 13:03
  • @extremeaxe5 But redirecting the end of the pipe redirects the stdout of the last program in the pipe. I think there will always be an ambiguity.
    – Jens
    Commented Aug 2, 2018 at 13:38
  • Actually, I'm wrong. It seems that the way to go with this is to put a time builtin in a command group, and then redirect the group. Redirecting the end of the pipe does nothing. Just try time echo hello 2> temp.txt, and observe that the timing information is still printed. Commented Aug 2, 2018 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.