Skip to main content

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

Example: Redirecting Output to a File

ls -l /home/user > directory_contents.txt

Example: Appending Output to a Log File

date >> system_log.txt
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

Example: Redirecting Errors to a Log File

ls /nonexistent_directory 2> error.log

Example: Merging Standard Output and Error

find /home -name "*.txt" 2>&1 > results.txt

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

Example: Redirecting both stdout and stderr to a file

ls /nonexistent 2&> error.log

Example: Redirecting both streams to /dev/null

command &> /dev/null

4. Redirecting Input

Operator Description Example
< Redirects input from a file sort < names.txt
<< Redirects input from a here document cat << EOF
Line 1
Line 2
EOF
0< Explicitly redirects stdin (file descriptor 0) command 0< input.txt

Example: Using input redirection with sort

sort < names.txt

Example: Using here document for input

cat << EOF
Hello World
This is a test
EOF

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.
command << EOF
Line 1
Line 2
EOF
Delimiter Can be any string, typically EOF, EOT, or END.
cat << END
Hello World
END
Quoting the delimiter Using quotes prevents variable expansion and command substitution.
cat << "EOF"
Hello $USER
EOF

Example: Writing a multi-line message to a file

cat << EOF > message.txt
This is line 1.
This is line 2.
EOF

Example: Using here document with a script

sh << EOF
echo "Hello, World!"
ls -l
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

Example: Discarding Both Standard Output and Error

command > /dev/null 2>&1

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.

Example: Reading specific lines from a file

sed -n '10,20p' file.txt

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

Example: Overwriting a file with command output

ls -l > file_list.txt

Example: Appending to a log file

date >> app.log
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

Example: Appending to a Log File

echo "Error: Connection failed" >> error.log
Note: Using >> ensures that new content is added to the end of the file without overwriting existing content.

Was this article helpful?