Working with Input and Output Redirection
1. Redirecting Standard Output
| Operator | Description | Example |
|---|---|---|
| > | Redirects standard output to a file, overwriting it | ls > file_list.txt |
| >> | Redirects standard output to a file, appending to it | echo "new line" >> log.txt |
| 2> | Redirects standard error to a file | command 2> errors.txt |
| 2>> | Appends standard error to a file | command 2>> errors.log |
| &> | Redirects both standard output and error to a file | command &> all_output.txt |
| &>> | Appends both standard output and error to a file | command &>> all_output.log |
Note: Redirecting output does not affect the command's display on the terminal. Use
tee if you want to both display and save output.
2. Redirecting Standard Error
| Redirection Operator | Description | Example |
|---|---|---|
| 2> | Redirects standard error to a file | command 2> error.log |
| 2>&1 | Redirects standard error to standard output | command 2>&1 |
| 2>/dev/null | Discards standard error output | command 2>/dev/null |
3. Redirecting Both Stdout and Stderr
| Redirection | Description | Example |
|---|---|---|
| 2>&1 | Redirects stderr to stdout, combining both streams | command 2>&1 |
| &> | Redirects both stdout and stderr to the same destination | command &> file.txt |
| 2>&1 > | Redirects stderr to stdout, then redirects stdout to a file | command 2>&1 > output.txt |
4. Redirecting Input
| Operator | Description | Example |
|---|---|---|
| < | Redirects input from a file | sort < names.txt |
| << | Redirects input from a here document | cat << EOFLine 1Line 2EOF |
| 0< | Explicitly redirects stdin (file descriptor 0) | command 0< input.txt |
5. Using Here Documents
A here document (or <<) is a way to provide multi-line input to a command directly in the shell. It allows you to pass a block of text as standard input without needing to create a temporary file.
| Feature | Description | Example |
|---|---|---|
| Basic syntax | Use << followed by a delimiter to start and end the input block. |
|
| Delimiter | Can be any string, typically EOF, EOT, or END. |
|
| Quoting the delimiter | Using quotes prevents variable expansion and command substitution. | |
Example: Writing a multi-line message to a file
cat << EOF > message.txt
This is line 1.
This is line 2.
EOF
Note: The delimiter must appear on a line by itself to end the here document. If you need to include the literal delimiter in the input, escape it or quote it.
6. Using Here Strings
A here string is a form of input redirection that allows you to pass a string directly as input to a command. It's similar to a here document but is used for single-line or small string inputs.
| Feature | Description | Example |
|---|---|---|
| Syntax | Uses <<< followed by the string |
grep "hello" <<< "hello world" |
| Usage | Passes a string directly to a command's stdin | cat <<< "This is a test" |
Example: Using Here Strings with grep
grep "linux" <<< "The Linux kernel is powerful"</code>
</pre>
</div>
</div>
<div class="example">
<h2 id="example-using-here-strings-with-cat">Example: Using Here Strings with cat<a href="#example-using-here-strings-with-cat" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<div class="codeBlock" data-language="bash">
<pre><code>cat <<< "This is a test string"</code>
</pre>
</div>
</div>
<h2 id="7-discarding-output">7. Discarding Output<a href="#7-discarding-output" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<table>
<tbody>
<tr>
<th>Operator</th>
<th>Description</th>
<th>Usage Example</th>
</tr>
<tr>
<td class="property">/dev/null</td>
<td>Special device file that discards all data written to it</td>
<td><code>command > /dev/null</code></td>
</tr>
</tbody>
</table>
<div class="example">
<h2 id="example-discarding-standard-output">Example: Discarding Standard Output<a href="#example-discarding-standard-output" class="toc__headingLink" aria-label="Link to this section"><svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<div class="codeBlock" data-language="bash"><pre><code>ls /nonexistent > /dev/null
8. Reading from Files
| Command | Description | Example |
|---|---|---|
| cat | Concatenates and displays file contents | cat file.txt |
| less | Opens file for paging through content | less large_file.txt |
| more | Displays file content one screen at a time | more file.txt |
| head | Displays first 10 lines of a file | head -n 5 file.txt |
| tail | Displays last 10 lines of a file | tail -n 5 file.txt |
Note: Use
cat for quick file viewing, and less or more for large files to avoid overwhelming terminal output.9. Writing to Files
| Operator | Description | Example |
|---|---|---|
| > | Overwrite file with output | echo "Hello" > output.txt |
| >> | Append output to file | echo "World" >> output.txt |
| < | Redirect file content as input | cat < input.txt |
Note: Using
> will overwrite the file if it exists. Use >> to append instead.
10. Appending to Files
| Operator | Description | Example |
|---|---|---|
| >> | Appends output to a file | echo "new line" >> logfile.txt |
| >> | Creates file if it doesn't exist | date >> output.log |
Note: Using
>> ensures that new content is added to the end of the file without overwriting existing content.