[CentOS] redirecting time output

Tue Nov 23 16:27:08 UTC 2010
Stephen Harris <lists at spuddy.org>

On Tue, Nov 23, 2010 at 05:04:09PM +0100, Rainer Traut wrote:
> Am 23.11.2010 15:30, schrieb m.roth at 5-cent.us:
> > Rainer Traut wrote:
> >> am trying to pipe output from time command and output from a shell
> >> script to the mail program.
> >> So far it's not working as expected...
> >>
> >> # time echo "test" 2>&1 | mail -s "timetest" my at mail.com

> >> The time command writes to stderror, but here the redirection seems to
> >> apply to the echo command?

> > Try `time echo "test"` etc. That way, it executes in a subshell, and has
> > one STDOUT and STDERR.

> Ok, yes this works.

The problem you're coming across is that "time" is a shell built-in and
not an external, so the shell is parsing "time foo 2>&1" as if it was
something similar to "time (foo 2>&1)".

The two common solutions are:
 1) explicitly use /usr/bin/time - eg
      /usr/bin/time -p foo 2>&1

 2) force a subshell - eg
      (time foo) 2>&1

-- 

rgds
Stephen