diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/bash/redirections.html | |
new repository
Diffstat (limited to 'devdocs/bash/redirections.html')
| -rw-r--r-- | devdocs/bash/redirections.html | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/devdocs/bash/redirections.html b/devdocs/bash/redirections.html new file mode 100644 index 00000000..bb303035 --- /dev/null +++ b/devdocs/bash/redirections.html @@ -0,0 +1,64 @@ +<h1 class="section">Redirections</h1> <p>Before a command is executed, its input and output may be <em>redirected</em> using a special notation interpreted by the shell. <em>Redirection</em> allows commands’ file handles to be duplicated, opened, closed, made to refer to different files, and can change the files the command reads from and writes to. Redirection may also be used to modify file handles in the current shell execution environment. The following redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right. </p> <p>Each redirection that may be preceded by a file descriptor number may instead be preceded by a word of the form {<var>varname</var>}. In this case, for each redirection operator except >&- and <&-, the shell will allocate a file descriptor greater than 10 and assign it to {<var>varname</var>}. If >&- or <&- is preceded by {<var>varname</var>}, the value of <var>varname</var> defines the file descriptor to close. If {<var>varname</var>} is supplied, the redirection persists beyond the scope of the command, allowing the shell programmer to manage the file descriptor’s lifetime manually. The <code>varredir_close</code> shell option manages this behavior (see <a href="the-shopt-builtin">The Shopt Builtin</a>). </p> <p>In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is ‘<samp><</samp>’, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is ‘<samp>></samp>’, the redirection refers to the standard output (file descriptor 1). </p> <p>The word following the redirection operator in the following descriptions, unless otherwise noted, is subjected to brace expansion, tilde expansion, parameter expansion, command substitution, arithmetic expansion, quote removal, filename expansion, and word splitting. If it expands to more than one word, Bash reports an error. </p> <p>Note that the order of redirections is significant. For example, the command </p> +<div class="example"> <pre class="example">ls > dirlist 2>&1 +</pre> +</div> <p>directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file <var>dirlist</var>, while the command </p> +<div class="example"> <pre class="example">ls 2>&1 > dirlist +</pre> +</div> <p>directs only the standard output to file <var>dirlist</var>, because the standard error was made a copy of the standard output before the standard output was redirected to <var>dirlist</var>. </p> <p>Bash handles several filenames specially when they are used in redirections, as described in the following table. If the operating system on which Bash is running provides these special files, bash will use them; otherwise it will emulate them internally with the behavior described below. </p> <dl compact> <dt><span><code>/dev/fd/<var>fd</var></code></span></dt> <dd> +<p>If <var>fd</var> is a valid integer, file descriptor <var>fd</var> is duplicated. </p> </dd> <dt><span><code>/dev/stdin</code></span></dt> <dd> +<p>File descriptor 0 is duplicated. </p> </dd> <dt><span><code>/dev/stdout</code></span></dt> <dd> +<p>File descriptor 1 is duplicated. </p> </dd> <dt><span><code>/dev/stderr</code></span></dt> <dd> +<p>File descriptor 2 is duplicated. </p> </dd> <dt><span><code>/dev/tcp/<var>host</var>/<var>port</var></code></span></dt> <dd> +<p>If <var>host</var> is a valid hostname or Internet address, and <var>port</var> is an integer port number or service name, Bash attempts to open the corresponding TCP socket. </p> </dd> <dt><span><code>/dev/udp/<var>host</var>/<var>port</var></code></span></dt> <dd><p>If <var>host</var> is a valid hostname or Internet address, and <var>port</var> is an integer port number or service name, Bash attempts to open the corresponding UDP socket. </p></dd> </dl> <p>A failure to open or create a file causes the redirection to fail. </p> <p>Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally. </p> <ul class="section-toc"> <li><a href="#Redirecting-Input" accesskey="1">Redirecting Input</a></li> <li><a href="#Redirecting-Output" accesskey="2">Redirecting Output</a></li> <li><a href="#Appending-Redirected-Output" accesskey="3">Appending Redirected Output</a></li> <li><a href="#Redirecting-Standard-Output-and-Standard-Error" accesskey="4">Redirecting Standard Output and Standard Error</a></li> <li><a href="#Appending-Standard-Output-and-Standard-Error" accesskey="5">Appending Standard Output and Standard Error</a></li> <li><a href="#Here-Documents" accesskey="6">Here Documents</a></li> <li><a href="#Here-Strings" accesskey="7">Here Strings</a></li> <li><a href="#Duplicating-File-Descriptors" accesskey="8">Duplicating File Descriptors</a></li> <li><a href="#Moving-File-Descriptors" accesskey="9">Moving File Descriptors</a></li> <li><a href="#Opening-File-Descriptors-for-Reading-and-Writing">Opening File Descriptors for Reading and Writing</a></li> </ul> <div class="subsection" id="Redirecting-Input"> <h4 class="subsection">3.6.1 Redirecting Input</h4> <p>Redirection of input causes the file whose name results from the expansion of <var>word</var> to be opened for reading on file descriptor <code>n</code>, or the standard input (file descriptor 0) if <code>n</code> is not specified. </p> <p>The general format for redirecting input is: </p> +<div class="example"> <pre class="example">[n]<word +</pre> +</div> </div> <div class="subsection" id="Redirecting-Output"> <h4 class="subsection">3.6.2 Redirecting Output</h4> <p>Redirection of output causes the file whose name results from the expansion of <var>word</var> to be opened for writing on file descriptor <var>n</var>, or the standard output (file descriptor 1) if <var>n</var> is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size. </p> <p>The general format for redirecting output is: </p> +<div class="example"> <pre class="example">[n]>[|]word +</pre> +</div> <p>If the redirection operator is ‘<samp>></samp>’, and the <code>noclobber</code> option to the <code>set</code> builtin has been enabled, the redirection will fail if the file whose name results from the expansion of <var>word</var> exists and is a regular file. If the redirection operator is ‘<samp>>|</samp>’, or the redirection operator is ‘<samp>></samp>’ and the <code>noclobber</code> option is not enabled, the redirection is attempted even if the file named by <var>word</var> exists. </p> </div> <div class="subsection" id="Appending-Redirected-Output"> <h4 class="subsection">3.6.3 Appending Redirected Output</h4> <p>Redirection of output in this fashion causes the file whose name results from the expansion of <var>word</var> to be opened for appending on file descriptor <var>n</var>, or the standard output (file descriptor 1) if <var>n</var> is not specified. If the file does not exist it is created. </p> <p>The general format for appending output is: </p> +<div class="example"> <pre class="example">[n]>>word +</pre> +</div> </div> <div class="subsection" id="Redirecting-Standard-Output-and-Standard-Error"> <h4 class="subsection">3.6.4 Redirecting Standard Output and Standard Error</h4> <p>This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of <var>word</var>. </p> <p>There are two formats for redirecting standard output and standard error: </p> +<div class="example"> <pre class="example">&>word +</pre> +</div> <p>and </p> +<div class="example"> <pre class="example">>&word +</pre> +</div> <p>Of the two forms, the first is preferred. This is semantically equivalent to </p> +<div class="example"> <pre class="example">>word 2>&1 +</pre> +</div> <p>When using the second form, <var>word</var> may not expand to a number or ‘<samp>-</samp>’. If it does, other redirection operators apply (see Duplicating File Descriptors below) for compatibility reasons. </p> </div> <div class="subsection" id="Appending-Standard-Output-and-Standard-Error"> <h4 class="subsection">3.6.5 Appending Standard Output and Standard Error</h4> <p>This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file whose name is the expansion of <var>word</var>. </p> <p>The format for appending standard output and standard error is: </p> +<div class="example"> <pre class="example">&>>word +</pre> +</div> <p>This is semantically equivalent to </p> +<div class="example"> <pre class="example">>>word 2>&1 +</pre> +</div> <p>(see Duplicating File Descriptors below). </p> </div> <div class="subsection" id="Here-Documents"> <h4 class="subsection">3.6.6 Here Documents</h4> <p>This type of redirection instructs the shell to read input from the current source until a line containing only <var>word</var> (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input (or file descriptor <var>n</var> if <var>n</var> is specified) for a command. </p> <p>The format of here-documents is: </p> +<div class="example"> <pre class="example">[n]<<[-]word + here-document +delimiter +</pre> +</div> <p>No parameter and variable expansion, command substitution, arithmetic expansion, or filename expansion is performed on <var>word</var>. If any part of <var>word</var> is quoted, the <var>delimiter</var> is the result of quote removal on <var>word</var>, and the lines in the here-document are not expanded. If <var>word</var> is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence <code>\newline</code> is ignored, and ‘<samp>\</samp>’ must be used to quote the characters ‘<samp>\</samp>’, ‘<samp>$</samp>’, and ‘<samp>`</samp>’. </p> <p>If the redirection operator is ‘<samp><<-</samp>’, then all leading tab characters are stripped from input lines and the line containing <var>delimiter</var>. This allows here-documents within shell scripts to be indented in a natural fashion. </p> </div> <div class="subsection" id="Here-Strings"> <h4 class="subsection">3.6.7 Here Strings</h4> <p>A variant of here documents, the format is: </p> +<div class="example"> <pre class="example">[n]<<< word +</pre> +</div> <p>The <var>word</var> undergoes tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal. Filename expansion and word splitting are not performed. The result is supplied as a single string, with a newline appended, to the command on its standard input (or file descriptor <var>n</var> if <var>n</var> is specified). </p> </div> <div class="subsection" id="Duplicating-File-Descriptors"> <h4 class="subsection">3.6.8 Duplicating File Descriptors</h4> <p>The redirection operator </p> +<div class="example"> <pre class="example">[n]<&word +</pre> +</div> <p>is used to duplicate input file descriptors. If <var>word</var> expands to one or more digits, the file descriptor denoted by <var>n</var> is made to be a copy of that file descriptor. If the digits in <var>word</var> do not specify a file descriptor open for input, a redirection error occurs. If <var>word</var> evaluates to ‘<samp>-</samp>’, file descriptor <var>n</var> is closed. If <var>n</var> is not specified, the standard input (file descriptor 0) is used. </p> <p>The operator </p> +<div class="example"> <pre class="example">[n]>&word +</pre> +</div> <p>is used similarly to duplicate output file descriptors. If <var>n</var> is not specified, the standard output (file descriptor 1) is used. If the digits in <var>word</var> do not specify a file descriptor open for output, a redirection error occurs. If <var>word</var> evaluates to ‘<samp>-</samp>’, file descriptor <var>n</var> is closed. As a special case, if <var>n</var> is omitted, and <var>word</var> does not expand to one or more digits or ‘<samp>-</samp>’, the standard output and standard error are redirected as described previously. </p> </div> <div class="subsection" id="Moving-File-Descriptors"> <h4 class="subsection">3.6.9 Moving File Descriptors</h4> <p>The redirection operator </p> +<div class="example"> <pre class="example">[n]<&digit- +</pre> +</div> <p>moves the file descriptor <var>digit</var> to file descriptor <var>n</var>, or the standard input (file descriptor 0) if <var>n</var> is not specified. <var>digit</var> is closed after being duplicated to <var>n</var>. </p> <p>Similarly, the redirection operator </p> +<div class="example"> <pre class="example">[n]>&digit- +</pre> +</div> <p>moves the file descriptor <var>digit</var> to file descriptor <var>n</var>, or the standard output (file descriptor 1) if <var>n</var> is not specified. </p> </div> <div class="subsection" id="Opening-File-Descriptors-for-Reading-and-Writing"> <h4 class="subsection">3.6.10 Opening File Descriptors for Reading and Writing</h4> <p>The redirection operator </p> +<div class="example"> <pre class="example">[n]<>word +</pre> +</div> <p>causes the file whose name is the expansion of <var>word</var> to be opened for both reading and writing on file descriptor <var>n</var>, or on file descriptor 0 if <var>n</var> is not specified. If the file does not exist, it is created. </p> </div><div class="_attribution"> + <p class="_attribution-p"> + Copyright © 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>Licensed under the GNU Free Documentation License.<br> + <a href="https://www.gnu.org/software/bash/manual/html_node/Redirections.html" class="_attribution-link">https://www.gnu.org/software/bash/manual/html_node/Redirections.html</a> + </p> +</div> |
