Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The example is right in practice. The book's original explanation is an accurate description of what the POSIX standard says, but the POSIX-like shells I have handy (<code>bash</code> and <code>dash</code>, the only ones I believe are commonly seen on Linux) are not that picky.</p> <p>The POSIX standard <a href="http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_07_05" rel="nofollow">says the same thing as the book</a> about input and output descriptors, and goes on to say this: for <code>n&lt;&amp;word</code>, "if the digits in <code>word</code> do not represent a file descriptor already open for input, a redirection error shall result". So if you want to be careful about POSIX compatibility, you should avoid this usage.</p> <p>The bash documentation also <a href="http://www.gnu.org/software/bash/manual/html_node/Redirections.html" rel="nofollow">says the same thing</a> about <code>&lt;&amp;</code> and <code>&gt;&amp;</code>, but without the promise of an error. Which is good, because it doesn't actually give an error. Instead, empirically <code>n&lt;&amp;m</code> and <code>n&gt;&amp;m</code> appear to be interchangeable. The only difference between <code>&lt;&amp;</code> and <code>&gt;&amp;</code> is that if you leave off the fd number on the left, <code>&lt;&amp;</code> defaults to 0 (stdin) and <code>&gt;&amp;</code> to 1 (stdout).</p> <p>For example, let's start a shell with fd 1 pointing at a file <code>bar</code>, then try out exactly the <code>exec 4&lt;&amp;1</code> example, try to write to the resulting fd 4, and see if it works:</p> <pre><code>$ sh -c 'exec 4&lt;&amp;1; echo foo &gt;&amp;4' &gt;bar; cat bar foo </code></pre> <p>It does, and this holds using either <code>dash</code> or <code>bash</code> (or <code>bash --posix</code>) for the shell.</p> <p>Under the hood, this makes sense because &lt;&amp; and >&amp; are almost certainly just calling <a href="http://linux.die.net/man/2/dup2" rel="nofollow">dup2()</a>, which doesn't care whether the fds are opened for reading or writing or appending or what.</p> <p>[<strong>EDIT</strong>: Added reference to POSIX after discussion in comments.]</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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