[clug] Redirect time command output to file

Kevin Pulo kev at pulo.com.au
Wed May 26 21:47:45 MDT 2010


On Thu, May 27, 2010 at 01:24:20PM +1000, Hal wrote:

> time appears to be a shell builtin command

Not quite.  time is actually a fairly special case - it's a reserved
word, and is treated differently to shell builtins (which are
more-or-less equivalent to functions/commands).

The bash manpage, specifically under "Shell Grammar", has the details.

Basically, you need to think of time as being "outside" of the entire
command - redirections, pipelines and all.  So while you may think of:

  $ time echo "foo" 2>&1 >>output.log

as being (bracketing here is just for emphasis):

  $ ( time echo "foo" ) 2>&1 >>output.log

bash actually treats it as:

  $ time ( echo "foo" 2>&1 >>output.log )

This is why there's no magic third output - it's using normal output,
but you just can't redirect it normally, that's all.

You could do:

  $ bash -c 'time echo "foo"' 2> time.out

but that will start to get a little meta with the special character
quoting.  Better is to use a sub-shell:

  $ ( time echo "foo" ) 2> time.out

If you need to grab the stderr from the actual command separately,
then grab it in the subshell:

  $ ( time foobar > foobar.out 2> foobar.err ) 2> time.out

> Ok Invoke the time binary explicitly

That's another option, but time(1) will behave differently to the bash
keyword.

> Why don't both "$ man time"and "$ help time" mention the  
> other given it is going to cause issues?

  $ type -all time

will tell you what you're getting.

Kev.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/linux/attachments/20100527/1c8183fa/attachment.pgp>


More information about the linux mailing list