diff options
Diffstat (limited to 'public/bash-redirection.html')
-rw-r--r-- | public/bash-redirection.html | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/public/bash-redirection.html b/public/bash-redirection.html index d4616fe..adeb9c9 100644 --- a/public/bash-redirection.html +++ b/public/bash-redirection.html @@ -10,8 +10,8 @@ <header><a href="/">Luke Shumaker</a> » <a href=/blog>blog</a> » bash-redirection</header> <article> <h1 id="bash-redirection">Bash redirection</h1> -<p>Apparently, too many people don't understand Bash redirection. They might get the basic syntax, but they think of the process as declarative; in Bourne-ish shells, it is procedural.</p> -<p>In Bash, streams are handled in terms of "file descriptors" of "FDs". FD 0 is stdin, FD 1 is stdout, and FD 2 is stderr. The equivalence (or lack thereof) between using a numeric file descriptor, and using the associated file in <code>/dev/*</code> and <code>/proc/*</code> is interesting, but beyond the scope of this article.</p> +<p>Apparently, too many people don’t understand Bash redirection. They might get the basic syntax, but they think of the process as declarative; in Bourne-ish shells, it is procedural.</p> +<p>In Bash, streams are handled in terms of “file descriptors” of “FDs”. FD 0 is stdin, FD 1 is stdout, and FD 2 is stderr. The equivalence (or lack thereof) between using a numeric file descriptor, and using the associated file in <code>/dev/*</code> and <code>/proc/*</code> is interesting, but beyond the scope of this article.</p> <h2 id="step-1-pipes">Step 1: Pipes</h2> <p>To quote the Bash manual:</p> <pre><code>A 'pipeline' is a sequence of simple commands separated by one of the @@ -19,7 +19,7 @@ control operators '|' or '|&'. The format for a pipeline is [time [-p]] [!] COMMAND1 [ [| or |&] COMMAND2 ...]</code></pre> -<p>Now, <code>|&</code> is just shorthand for <code>2>&1 |</code>, the pipe part happens here, but the <code>2>&1</code> part doesn't happen until step 2.</p> +<p>Now, <code>|&</code> is just shorthand for <code>2>&1 |</code>, the pipe part happens here, but the <code>2>&1</code> part doesn’t happen until step 2.</p> <p>First, if the command is part of a pipeline, the pipes are set up. For every instance of the <code>|</code> metacharacter, Bash creates a pipe (<code>pipe(3)</code>), and duplicates (<code>dup2(3)</code>) the write end of the pipe to FD 1 of the process on the left side of the <code>|</code>, and duplicate the read end of the pipe to FD 0 of the process on the right side.</p> <h2 id="step-2-redirections">Step 2: Redirections</h2> <p><em>After</em> the initial FD 0 and FD 1 fiddling by pipes is done, Bash looks at the redirections. <strong>This means that redirections can override pipes.</strong></p> |