Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Redirect output of time builtin

On Jan 30,  4:39pm, Matthias Vallentin wrote:
} Is it possible to redirect output from the time builtin?

Sure.  It's printed to stderr.  The trick is that it's parsed as if

    time { pipeline including redirects }

so to redirect the output of time rather than the output of pipeline,
you must put in your own braces:

    { time pipeline including redirects } 2> stderr.txt

In order to split the stderr of time from the stderr of the pipeline,
you need to get a little trickier:

    { time simple command 2>&3 } 3>&2 2>time.txt

It might be easier to see what's going on there if you write the
redirections in the order that they are actually performed by the
shell when executing the command:

    3>&2 2>time.txt { time 2>&3 simple command }

Thus, 3 becomes the same as 2, then 2 goes to the file time.txt
(leaving 3 pointing where 2 used to point), then time implicitly
grabs 2 for its stderr, then 2 becomes the same as 3 (back to the
original 2 again, but leaving time's stderr directed to time.txt),
and finally the simple command runs with 2 as its stderr.

I replaced the pipeline with a simple command to keep the explanation
short; if you have an actual pipeline rather than a simple command,
placement of the 2>&3 becomes more complicated.

Additional note:  The time builtin applied to any construct that is
executed in the current shell, is silently ignored.  So although
it's syntactically OK to put an opening curly or a repeat-loop or
the like immediately after the time keyword, you'll get no timing
statistics.  You have to use parens instead, to force a subshell,
which is then timed.

Messages sorted by: Reverse Date, Date, Thread, Author