title: How to get/parse the "target" of a redirect in (ba)sh command
body: <p>I have to run a binary through a simulator, but the binary is part of a huge and complicated benchmarking suite. So what on the normal system would look like this:</p>
  <pre><code># First form
  ./binary -long=list -of=args &lt;input.txt &gt;std.out 2&gt;std.err
  </code></pre>
  <p>needs to look like this:</p>
  <pre><code># second form
  simulator -c ./binary -o &quot;-long=list -of=args&quot; -i input.txt --output=std.out --errout=std.err 
  </code></pre>
  <p>The benchmarking suit is passing around the command, i.e. the binary with all the stuff after it (args and redirects) in (bash) variables e.g. <code>$CMD=&quot;./binary -long=list -of=args &lt;input.txt &gt;std.out 2&gt;std.err&quot;</code>, and I would need to find out what are the targets of all the redirects (i.e. <code>intpu.txt</code>, <code>std.out</code> and <code>std.err</code> in the example) so I can convert the command in the first form to the command in the second form).</p>
  <p>So is there a way to, for example, tell bash to take the command <code>$CMD</code>, which contains the binary, the args and the redirects, and parse it, and then return, say, what was the input redirect (i.e. <code>input.txt</code>), or one of the other redirects (<code>std.out</code> or <code>std.err</code>)?</p>
  <p>Without manually doing the parsing of course...</p>
  <p>Something like this:</p>
  <pre><code>$ CMD=&quot;./binary -long=list -of=args &lt;input.txt &gt;std.out 2&gt;std.err&quot;
  $ bash -secret_option_to_parse_redirect_target=input &quot;$CMD&quot;
  intpu.txt
  $ bash -secret_option_to_parse_redirect_target=output &quot;$CMD&quot;
  std.out
  $ bash -secret_option_to_parse_redirect_target=error &quot;$CMD&quot;
  std.err
  </code></pre>