<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Output Redirection With Bash</title> <atom:link href="http://www.skorks.com/2009/09/output-redirection-with-bash/feed/" rel="self" type="application/rss+xml" /><link>http://www.skorks.com/2009/09/output-redirection-with-bash/</link> <description>For the betterment of the software craft...</description> <lastBuildDate>Mon, 21 Nov 2011 13:57:06 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.2</generator> <item><title>By: Maxim</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-5022</link> <dc:creator>Maxim</dc:creator> <pubDate>Tue, 04 May 2010 21:44:36 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-5022</guid> <description>Just a small observation : as I&#039;m using DASH instead of BASH (for better portability and POSIX compliance) I had to close the file descriptor #3 explicitly with 3&gt;&amp;- instead of the more compact 2&gt;&amp;3- form that BASH allowed. So it would now looks like : 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 3&gt;&amp;- in my DASH compatible script.</description> <content:encoded><![CDATA[<p>Just a small observation : as I&#8217;m using DASH instead of BASH (for better portability and POSIX compliance) I had to close the file descriptor #3 explicitly with 3&gt;&amp;- instead of the more compact 2&gt;&amp;3- form that BASH allowed. So it would now looks like : 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3 3&gt;&amp;- in my DASH compatible script.</p> ]]></content:encoded> </item> <item><title>By: Maxim</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-5003</link> <dc:creator>Maxim</dc:creator> <pubDate>Tue, 04 May 2010 17:41:55 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-5003</guid> <description>Thanks, Alan!
Your explanation did not only enabled my script to do exactly what I wanted, but I think I now fully understand how bash&#039;s file descriptors can be used. :)
Kudos! Have a nice week.</description> <content:encoded><![CDATA[<p>Thanks, Alan!</p><p>Your explanation did not only enabled my script to do exactly what I wanted, but I think I now fully understand how bash&#8217;s file descriptors can be used. :)</p><p>Kudos! Have a nice week.</p> ]]></content:encoded> </item> <item><title>By: Alan Skorkin</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-4941</link> <dc:creator>Alan Skorkin</dc:creator> <pubDate>Tue, 04 May 2010 02:08:09 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-4941</guid> <description>Glad you found it useful. What you want to do is not too difficult, infact I&#039;ve written about something similar before. You can &lt;a href=&quot;http://www.skorks.com/2009/09/using-bash-to-output-to-screen-and-file-at-the-same-time/&quot; rel=&quot;nofollow&quot;&gt;check out my other post on redirection&lt;/a&gt;, but here is basically what you want to do.
The tee command is your friend (I explain it the post I linked to above). What you have to do is, swap stdout and stderr around so that you can pipe stderr to tee, that&#039;s pretty much it. Here is a sample command:
&lt;pre&gt;ls -al 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3- &#124; tee blah.txt&lt;/pre&gt;
If you run that one you will get your output on the screen but nothing in the file, since no error was produced. But if you run this:
&lt;pre&gt;ls -garbage 3&gt;&amp;1 1&gt;&amp;2 2&gt;&amp;3- &#124; tee blah.txt&lt;/pre&gt;
You will get your error output both on the screen and in the file, just as you wanted. The reason you have to swap stdout and stderr around is because you can only use pipes with stdout and you need to pipe your output to tee to get it in a file and on the screen. Hope this helps.</description> <content:encoded><![CDATA[<p>Glad you found it useful. What you want to do is not too difficult, infact I&#8217;ve written about something similar before. You can <a
href="http://www.skorks.com/2009/09/using-bash-to-output-to-screen-and-file-at-the-same-time/" rel="nofollow">check out my other post on redirection</a>, but here is basically what you want to do.</p><p>The tee command is your friend (I explain it the post I linked to above). What you have to do is, swap stdout and stderr around so that you can pipe stderr to tee, that&#8217;s pretty much it. Here is a sample command:</p><pre>ls -al 3>&#038;1 1>&#038;2 2>&#038;3- | tee blah.txt</pre><p>If you run that one you will get your output on the screen but nothing in the file, since no error was produced. But if you run this:</p><pre>ls -garbage 3>&#038;1 1>&#038;2 2>&#038;3- | tee blah.txt</pre><p>You will get your error output both on the screen and in the file, just as you wanted. The reason you have to swap stdout and stderr around is because you can only use pipes with stdout and you need to pipe your output to tee to get it in a file and on the screen. Hope this helps.</p> ]]></content:encoded> </item> <item><title>By: Maxim</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-4940</link> <dc:creator>Maxim</dc:creator> <pubDate>Tue, 04 May 2010 01:33:15 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-4940</guid> <description>First, let me thank you for this bit of info. It&#039;s one of the best articulated information on redirection I&#039;ve found around. So thank you.
However, it still does not answer one of the case I&#039;m trying to implement : direct only stderr to a file, and print both stderr and stdout to console.
Would you happen to know a simple way to do this?  Thanks in advance!</description> <content:encoded><![CDATA[<p>First, let me thank you for this bit of info. It&#8217;s one of the best articulated information on redirection I&#8217;ve found around. So thank you.</p><p>However, it still does not answer one of the case I&#8217;m trying to implement : direct only stderr to a file, and print both stderr and stdout to console.</p><p>Would you happen to know a simple way to do this?  Thanks in advance!</p> ]]></content:encoded> </item> <item><title>By: Ray</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-3744</link> <dc:creator>Ray</dc:creator> <pubDate>Sun, 28 Feb 2010 14:46:00 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-3744</guid> <description>Hi. Your explanation was very clear and to the point. Thank you for this. I also like to ask you to add some light to the redirection with pipe and exec commands if possible.
Ray</description> <content:encoded><![CDATA[<p>Hi. Your explanation was very clear and to the point. Thank you for this. I also like to ask you to add some light to the redirection with pipe and exec commands if possible.</p><p>Ray</p> ]]></content:encoded> </item> <item><title>By: Alan Skorkin</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-3608</link> <dc:creator>Alan Skorkin</dc:creator> <pubDate>Mon, 22 Feb 2010 05:52:32 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-3608</guid> <description>If you simply want stderr printed to the screen, then that is what the shell will do by default, so if you need to discard stdout but print stderr to the console, all you have to do is redirect stdout to /dev/null:
ls -al &gt; /dev/null
There is no need to to point stderr to stdout (2&gt;&amp;1) as by default they both go to the console. Of course the above command will print nothing as it will succeed and output will go to /dev/null. But if you cause the command to fail you will get output on the console.</description> <content:encoded><![CDATA[<p>If you simply want stderr printed to the screen, then that is what the shell will do by default, so if you need to discard stdout but print stderr to the console, all you have to do is redirect stdout to /dev/null:</p><p>ls -al > /dev/null</p><p>There is no need to to point stderr to stdout (2>&#038;1) as by default they both go to the console. Of course the above command will print nothing as it will succeed and output will go to /dev/null. But if you cause the command to fail you will get output on the console.</p> ]]></content:encoded> </item> <item><title>By: Tiago</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-3596</link> <dc:creator>Tiago</dc:creator> <pubDate>Sun, 21 Feb 2010 14:04:04 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-3596</guid> <description>Thanks for the short but clear explanation.
In my case, I need just stderr printed, and discard stdout. I&#039;ve found the following way of doing that:
ls -al 2&gt;&amp;1 1&gt;/dev/null
Is there a better way? Or, even if not better, other simple way of doing the same thing?</description> <content:encoded><![CDATA[<p>Thanks for the short but clear explanation.<br
/> In my case, I need just stderr printed, and discard stdout. I&#8217;ve found the following way of doing that:<br
/> ls -al 2&gt;&amp;1 1&gt;/dev/null</p><p>Is there a better way? Or, even if not better, other simple way of doing the same thing?</p> ]]></content:encoded> </item> <item><title>By: Alan Skorkin</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-2966</link> <dc:creator>Alan Skorkin</dc:creator> <pubDate>Tue, 22 Sep 2009 06:56:05 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-2966</guid> <description>Yeah, that&#039;s the best way.</description> <content:encoded><![CDATA[<p>Yeah, that&#8217;s the best way.</p> ]]></content:encoded> </item> <item><title>By: Thura</title><link>http://www.skorks.com/2009/09/output-redirection-with-bash/comment-page-1/#comment-2965</link> <dc:creator>Thura</dc:creator> <pubDate>Tue, 22 Sep 2009 06:54:34 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1210#comment-2965</guid> <description>I just use ls -al &amp;&gt; file.txt to redirect both stdout &amp; stderr ...</description> <content:encoded><![CDATA[<p>I just use ls -al &amp;&gt; file.txt to redirect both stdout &amp; stderr &#8230;</p> ]]></content:encoded> </item> </channel> </rss>
