{"id":366,"date":"2017-08-11T11:33:40","date_gmt":"2017-08-11T10:33:40","guid":{"rendered":"https:\/\/www.weeklywhinge.com\/?p=366"},"modified":"2017-08-15T09:55:50","modified_gmt":"2017-08-15T08:55:50","slug":"how-to-redirect-stdout-to-one-pipe-and-stderr-to-another-in-bash-or-bourne-shell","status":"publish","type":"post","link":"https:\/\/www.weeklywhinge.com\/?p=366","title":{"rendered":"How to redirect stdout to one pipe and stderr to another in bash or bourne shell"},"content":{"rendered":"<p>So I couldn&#8217;t find an answer to this when I searched for it&#8230; how can I redirect stdout to one pipe and stderr to another?<\/p>\n<p>In my head, it seems like you should be able to do<\/p>\n<pre>mycommand |stdoutpipecmd 2|stderrpipecmd<\/pre>\n<p>but you can&#8217;t.<\/p>\n<p>There are many answers knocking around stackexchange that seem to not-quite do\u00c2\u00a0what I wanted, plus in bash you can apparently do command-redirection with 2&gt;(stderrpipecmd); however I was using bourne shell (embedded system) so that didn&#8217;t help me.<\/p>\n<p>&nbsp;<\/p>\n<p>Of course the answer is really ridiculously simple: simply encapsulate the redirect into a subshell, and redirect the stderr of the subshell into stdout and pipe\u00c2\u00a0<em>that<\/em>.<\/p>\n<p>So<\/p>\n<pre>( mycommand | stdoutpipecmd ) 2&gt;&amp;1 | stderrpipecmd<\/pre>\n<p>It&#8217;s really that simple.<\/p>\n<p>&nbsp;<\/p>\n<p>If you want to make the result of stdoutpipecmd <em>not<\/em> be piped through stderrpipecmd (you probably don&#8217;t), you can do<\/p>\n<p>&nbsp;<\/p>\n<pre>( mycommand | stdoutpipecmd 1&gt;&amp;3 ) 3&gt;&amp;2 2&gt;&amp;1 | stderrpipecmd<\/pre>\n<p>which will pipe the output from stdoutpipecmd to stderr, while the output from stderrpipecmd will be on stdout.<\/p>\n<p>To fix\u00c2\u00a0<em>that<\/em>, so the output from stdoutpipecmd ends up back on stdout while the output from stderrpipecmd is on stderr, it gets a bit messier:<\/p>\n<pre>( ( mycommand | stdoutpipecmd 1&gt;&amp;3 ) 2&gt;&amp;1 | stderrpipecmd ) 3&gt;&amp;1 1&gt;&amp;2<\/pre>\n<p>What fun \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I couldn&#8217;t find an answer to this when I searched for it&#8230; how can I redirect stdout to one pipe and stderr to another? In my head, it seems like you should be able to do mycommand |stdoutpipecmd 2|stderrpipecmd but you can&#8217;t. There are many answers knocking around stackexchange that seem to not-quite do\u00c2\u00a0what [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[79,80,77,78],"class_list":["post-366","post","type-post","status-publish","format-standard","hentry","category-tech","tag-bourne-shell","tag-linux","tag-pipes","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=366"}],"version-history":[{"count":4,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/366\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/366\/revisions\/370"}],"wp:attachment":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}