Note that there are some explanatory texts on larger screens.

plurals
  1. POBash-Scripting stderr and stdout
    primarykey
    data
    text
    <p>I am new to bash-scripting &amp; trying to understand how things work. It's all a bit strange.. <br/> I have two scripts. First this one:</p> <pre><code>#!/usr/bin/bash #name: stderrtest0.sh echo "on ${1}: this is an error" &gt;&amp;2 echo "on ${1}: this is an info" &gt;&amp;1 echo "on ${1}: this is just text" </code></pre> <p><br/> And this second script calls the first:</p> <pre><code>#!/usr/bin/bash #name: stderrtest1.sh echo "invoking: stderrtest0.sh test1 &gt;&amp;2 ~output:" ./stderrtest0.sh test1 &gt;&amp;2 echo "invoking: stderrtest0.sh test2 &gt;&amp;1 ~output:" ./stderrtest0.sh test2 &gt;&amp;1 echo "invoking: stderrtest0.sh test3 2&gt;&amp;1 ~output:" ./stderrtest0.sh test3 2&gt;&amp;1 echo "invoking: stderrtest0.sh test4 1&gt;&amp;2 ~output:" ./stderrtest0.sh test4 1&gt;&amp;2 echo "invoking: stderrtest0.sh test5 ~output:" ./stderrtest0.sh test5 </code></pre> <p><br/> Here are my tests (<em>debian squeeze</em>) with the output:</p> <p><strong>METATEST1)</strong> invoke <em>stderrtest1.sh</em></p> <pre><code>$ ./stderrtest1.sh invoking: stderrtest0.sh test1 &gt;&amp;2 ~output: on test1: this is an error on test1: this is an info on test1: this is just text invoking: stderrtest0.sh test2 &gt;&amp;1 ~output: on test2: this is an error on test2: this is an info on test2: this is just text invoking: stderrtest0.sh test3 2&gt;&amp;1 ~output: on test3: this is an error on test3: this is an info on test3: this is just text invoking: stderrtest0.sh test4 1&gt;&amp;2 ~output: on test4: this is an error on test4: this is an info on test4: this is just text invoking: stderrtest0.sh test5 ~output: on test5: this is an error on test5: this is an info on test5: this is just text </code></pre> <p>This is as I expect. Since by default <code>stderr</code> &amp; <code>stdout</code> get sent to the terminal.</p> <p><br/> <strong>METATEST2)</strong> invoke <em>stderrtest1.sh</em> &amp; redirect output to <em>out</em></p> <pre><code>$ ./stderrtest1.sh &gt;out on test1: this is an error on test1: this is an info on test1: this is just text on test2: this is an error on test4: this is an error on test4: this is an info on test4: this is just text on test5: this is an error $ cat out invoking: stderrtest0.sh test1 &gt;&amp;2 ~output: invoking: stderrtest0.sh test2 &gt;&amp;1 ~output: on test2: this is an info on test2: this is just text invoking: stderrtest0.sh test3 2&gt;&amp;1 ~output: on test3: this is an error on test3: this is an info on test3: this is just text invoking: stderrtest0.sh test4 1&gt;&amp;2 ~output: invoking: stderrtest0.sh test5 ~output: on test5: this is an info on test5: this is just text </code></pre> <p>So here:</p> <ul> <li>all <code>stdout</code> gets sent to the file <em>out</em> </li> <li>all <code>stderr</code> gets sent to the terminal</li> </ul> <p>This is not quite as I expect. I somehow thought everything might end up in <em>out</em></p> <p><br/> <strong>METATEST3)</strong> invoke <em>stderrtest1.sh</em> &amp; redirect <code>stdout</code> to <em>inf.out</em></p> <pre><code>$ ./stderrtest1.sh 1&gt;inf.out on test1: this is an error on test1: this is an info on test1: this is just text on test2: this is an error on test4: this is an error on test4: this is an info on test4: this is just text on test5: this is an error $ cat inf.out invoking: stderrtest0.sh test1 &gt;&amp;2 ~output: invoking: stderrtest0.sh test2 &gt;&amp;1 ~output: on test2: this is an info on test2: this is just text invoking: stderrtest0.sh test3 2&gt;&amp;1 ~output: on test3: this is an error on test3: this is an info on test3: this is just text invoking: stderrtest0.sh test4 1&gt;&amp;2 ~output: invoking: stderrtest0.sh test5 ~output: on test5: this is an info on test5: this is just text </code></pre> <p>Results are identical to <strong>METATEST2</strong>:</p> <ul> <li>all <code>stdout</code> gets sent to the file <em>inf.out</em></li> <li>all <code>stderr</code> gets sent to the terminal</li> </ul> <p>Ok. Now I understand <strong>METATEST2</strong>. Redirecting without specification defaults to <code>stdout</code>.</p> <p><br/> <strong>METATEST4)</strong> invoke <em>stderrtest1.sh</em> &amp; redirect <code>stderr</code> to <em>err.out</em></p> <pre><code>$ ./stderrtest1.sh 2&gt;err.out invoking: stderrtest0.sh test1 &gt;&amp;2 ~output: invoking: stderrtest0.sh test2 &gt;&amp;1 ~output: on test2: this is an info on test2: this is just text invoking: stderrtest0.sh test3 2&gt;&amp;1 ~output: on test3: this is an error on test3: this is an info on test3: this is just text invoking: stderrtest0.sh test4 1&gt;&amp;2 ~output: invoking: stderrtest0.sh test5 ~output: on test5: this is an info on test5: this is just text $ cat err.out on test1: this is an error on test1: this is an info on test1: this is just text on test2: this is an error on test4: this is an error on test4: this is an info on test4: this is just text on test5: this is an error </code></pre> <p>And here I get confused. Because in <strong>METATEST3 test1:</strong><br/> all output from <em>stderrtest0.sh</em> gets redirected to <code>stderr</code> &amp; so goes to the terminal but not to <em>inf.out</em></p> <p>And yet here in <strong>METATEST4 test2:</strong><br/> all output from <em>stderrtest1.sh</em> is being redirected to <code>stdout</code> ~BUT the <code>stderr</code> emitted by <em>stderrtest0.sh</em> somehow escapes ?</p> <p><br/> So this seems to imply:</p> <ul> <li>any/all output from some called program can be redirected into the caller's <code>stderr</code>. </li> <li><code>stderr</code> output from some called program is not redirected into the caller's <code>stdout</code>.</li> </ul> <p><br/> Is this the case?<br/></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload