Differences between revisions 3 and 4
Revision 3 as of 2007-08-30 03:05:44
Size: 1643
Editor: GreyCat
Comment: clean up and clarify
Revision 4 as of 2008-05-16 18:57:42
Size: 1634
Editor: GreyCat
Comment: add a couple links
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
Because of this, people who want to redirect {{{time}}}'s output often encounter difficulty figuring out where all the file descriptors are going. It's not as hard as most people think, though -- the trick is to call {{{time}}} in a different shell or block, and redirect stderr of ''that'' shell or block (which will contain {{{time}}}'s results). If you need to redirect the actual command's stdout or stderr, you do that inside the inner shell/block. For example: Because of this, people who want to redirect {{{time}}}'s output often encounter difficulty figuring out where all the file descriptors are going. It's not as hard as most people think, though -- the trick is to call {{{time}}} in a SubShell or block, and then capture stderr of the subshell or block (which will contain {{{time}}}'s results). If you need to redirect the actual command's stdout or stderr, you do that inside the subshell/block. For example:
Line 22: Line 22:
 * Command substitution:  * CommandSubstitution:

Anchor(faq32)

How can I redirect the output of 'time' to a variable or file?

Bash's time keyword uses special trickery, so that you can do things like

   time find ... | xargs ...

and get the execution time of the entire pipeline, rather than just the simple command at the start of the pipe. (This is different from the behavior of the external command time(1), for obvious reasons.)

Because of this, people who want to redirect time's output often encounter difficulty figuring out where all the file descriptors are going. It's not as hard as most people think, though -- the trick is to call time in a SubShell or block, and then capture stderr of the subshell or block (which will contain time's results). If you need to redirect the actual command's stdout or stderr, you do that inside the subshell/block. For example:

  • File redirection:
       bash -c "time ls" 2>time.output      # Explicit, but inefficient.
       ( time ls ) 2>time.output            # Slightly more efficient.
       { time ls; } 2>time.output           # Most efficient.
    
       # The general case:
       { time some command >stdout 2>stderr; } 2>time.output
  • CommandSubstitution:

       foo=$( bash -c "time ls" 2>&1 )       # Captures *everything*.
       foo=$( { time ls; } 2>&1 )            # More efficient version.
    
       # Keep stdout unmolested.
       exec 3>&1
       foo=$( { time bar 1>&3; } 2>&1 )      # Captures stderr and time.
       exec 3>&-
    
       # Keep both stdout and stderr unmolested.
       exec 3>&1 4>&2
       foo=$( { time bar 1>&3 2>&4; } 2>&1 )  # Captures time only.
       exec 3>&- 4>&-

BashFAQ/032 (last edited 2018-07-26 21:28:38 by GreyCat)