Differences between revisions 11 and 13 (spanning 2 versions)
Revision 11 as of 2012-12-12 18:30:13
Size: 2175
Editor: c-24-21-170-126
Comment: add note for pipes
Revision 13 as of 2017-02-23 21:36:09
Size: 2548
Editor: GreyCat
Comment: job control message suppression, and remove unneeded indentation
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
   time find ... | xargs ... time find ... | xargs ...
Line 14: Line 14:
   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.
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.
Line 18: Line 18:
   # The general case:
   { time some command >stdout 2>stderr; } 2>time.output
}}}

 * Pipe redirection
 {{{
    { { time ls; } 2>&1; } | gzip - # Seems to work
# The general case:
{ time some command >stdout 2>stderr; } 2>time.output
Line 29: Line 24:
   foo=$( bash -c "time ls" 2>&1 ) # Captures *everything*.
   foo=$( { time ls; } 2>&1 ) # More efficient version.
foo=$( bash -c "time ls" 2>&1 ) # Captures *everything*.
foo=$( { time ls; } 2>&1 ) # More efficient version.
Line 32: Line 27:
   # Keep stdout unmolested.
   exec 3>&1
   foo=$( { time bar 1>&3; } 2>&1 ) # Captures stderr and time.
   exec 3>&-
# Keep stdout unmolested.
exec 3>&1
foo=$( { time bar 1>&3; } 2>&1 ) # Captures stderr and time.
exec 3>&-
Line 37: Line 32:
   # 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>&-
# 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>&-
Line 42: Line 37:
   # same thing without exec
   { foo=$( { time bar 1>&3- 2>&4-; } 2>&1 ); } 3>&1 4>&2
# same thing without exec
{ foo=$( { time bar 1>&3- 2>&4-; } 2>&1 ); } 3>&1 4>&2
}}}

 * Pipe:
 {{{
# Make time only output elapsed time in seconds
TIMEFORMAT=%R
# Keep stdout and stderr unmolested
exec 3>&1 4>&2
{ time foo 1>&3 2>&4; } 2>&1 | awk '{
    printf "The task took %d hours, %d minutes and %.3f seconds\n",
           $1/3600, $1%3600/60, $1%60
}'
exec 3>&- 4>&-
Line 49: Line 57:
   ./coredump >log 2>&1 # Fails to capture the message
   { ./coredump; } >log 2>&1 # Captures the message
./coredump >log 2>&1 # Fails to capture the message
{ ./coredump; } >log 2>&1 # Captures the message
Line 52: Line 60:

The same applies to job control messages:

{{{
$ { sleep 1 & } >log 2>&1
$ cat log
[1] 10316
[1]+ Done sleep 1
}}}

Of course you may opt to redirect to `/dev/null` instead of a file.

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>&-
    
    # same thing without exec
    { foo=$( { time bar 1>&3- 2>&4-; } 2>&1 ); } 3>&1 4>&2
  • Pipe:
    # Make time only output elapsed time in seconds
    TIMEFORMAT=%R
    # Keep stdout and stderr unmolested
    exec 3>&1 4>&2
    { time foo 1>&3 2>&4; } 2>&1 | awk '{
        printf "The task took %d hours, %d minutes and %.3f seconds\n",
               $1/3600, $1%3600/60, $1%60
    }'
    exec 3>&- 4>&-

A similar construct can be used to capture "core dump" messages, which are actually printed by the shell that launched a program, not by the program that just dumped core:

./coredump >log 2>&1           # Fails to capture the message
{ ./coredump; } >log 2>&1      # Captures the message

The same applies to job control messages:

$ { sleep 1 & } >log 2>&1
$ cat log
[1] 10316
[1]+  Done                    sleep 1

Of course you may opt to redirect to /dev/null instead of a file.


CategoryShell

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