title: How to capture stdout/stderr/time into variables in Zsh
body: <p>Suppose the following function exists.</p>
  
  <pre><code>action() {
      sleep 1
      echo 'stdout'
      &gt;&amp;2 echo 'stderr'
  }
  </code></pre>
  
  <p>I want to capture each of the following items into shell variables.</p>
  
  <ul>
  <li>stdout</li>
  <li>stderr</li>
  <li>execution time</li>
  </ul>
  
  <p>The following code does the trick only in Bash. </p>
  
  <pre><code>unset stdout stderr time
  eval "$(
      { time action &gt;&amp;3 2&gt;&amp;4; } \
      2&gt; &gt;(time=$(cat); typeset -p time) \
      3&gt; &gt;(stdout=$(cat); typeset -p stdout) \
      4&gt; &gt;(stderr=$(cat); typeset -p stderr)
  )"
  
  echo "stdout: $stdout"
  echo "stderr: $stderr"
  echo "time: $time"
  </code></pre>
  
  <p>How can we achieve in Zsh?</p>