title: POSIX shell (sh) redirect stderr to stdout and capture stderr and stdout into variables
body: <p>I would like to redirect stderr to stdout so a terminal prints both of them during a command execution but I would like also to capture both of them into separate variables. I managed to achieve that in Bash (version 4.4.20(1)-release):</p>
  <pre><code>#!/bin/bash
  
  echo &quot;terminal:&quot;
  { err=&quot;$(find / -maxdepth 2 -iname 'tmp' -type d 2&gt;&amp;1 1&gt;&amp;3 3&gt;&amp;- | tee /dev/stderr)&quot;; ec=&quot;$?&quot;; } 3&gt;&amp;1 | tee /dev/fd/4 2&gt;&amp;1; out=$(cat /dev/fd/4)
  echo &quot;stdout:&quot; &amp;&amp; echo &quot;$out&quot;
  echo &quot;stderr:&quot; &amp;&amp; echo &quot;$err&quot;
  </code></pre>
  <p>that gives desired:</p>
  <pre><code>terminal:
  find: ‘/root’: Permission denied
  /tmp
  /var/tmp
  find: ‘/lost+found’: Permission denied
  stdout:
  /tmp
  /var/tmp
  stderr:
  find: ‘/root’: Permission denied
  find: ‘/lost+found’: Permission denied
  </code></pre>
  <p>but I have a problem converting that script into POSIX sh <code>/bin/sh</code></p>
  <pre><code>#!/bin/sh
  
  echo &quot;terminal:&quot;
  { err=&quot;$(find / -maxdepth 2 -iname 'tmp' -type d 2&gt;&amp;1 1&gt;&amp;3 3&gt;&amp;- | tee /dev/stderr)&quot;; ec=&quot;$?&quot;; } 3&gt;&amp;1 | tee /dev/fd/4 2&gt;&amp;1; out=$(cat /dev/fd/4)
  echo &quot;stdout:&quot; &amp;&amp; echo &quot;$out&quot;
  echo &quot;stderr:&quot; &amp;&amp; echo &quot;$err&quot;
  </code></pre>
  <p>gives:</p>
  <pre><code>terminal:
  tee: /dev/fd/4: No such file or directory
  find: ‘/root’: Permission denied
  /tmp
  /var/tmp
  find: ‘/lost+found’: Permission denied
  cat: /dev/fd/4: No such file or directory
  stdout:
  
  stderr:
  </code></pre>
  <p><code>/dev/fd/4</code> does not exist, and there is no <code>/proc/self/fd/4</code> either.</p>
  <p>How to make that script working as a POSIX shell script?</p>