| |

LFCS 25 ๐Ÿง Output Redirection โ€” >, >>, 2>, &>

The previous chapter introduced the three standard streams and the operators that control them. This chapter goes deeper into the output redirection specifically: the >, the >>, the 2>, the 2>>, the &>, the &>>, and the 2>&1 that ties them together. The output redirection is the mechanism that captures the output of a command into a file, appends it to a log, separates the data from the errors, or discards the noise entirely. The LFCS exam tests it as part of the “Use streams, pipes, and redirects” objective, and the exam scenarios include the log capture, the error separation, and the pipeline composition. The operators are simple in isolation, but the combination and the order are the source of the subtle bugs.

The distinction between the stdout and the stderr is the foundation. The > operates on the stdout by default, and the 2> operates on the stderr. The &> operates on both. The 2>&1 is not a file redirection; it is a descriptor duplication. It copies the current target of the stdout to the stderr, so the two point to the same place. The order matters: the > file 2>&1 points the stdout to the file first, then duplicates it to the stderr, so both go to the file. The 2>&1 > file duplicates the stdout (the terminal) to the stderr first, then points the stdout to the file, so the stderr goes to the terminal and the stdout goes to the file.

The truncation versus the append is the second distinction. The > truncates the file, and the >> appends. The > creates the file if it does not exist, and the >> does the same. The > with the noclobber option fails if the file exists, and the >| forces the write. The set -o noclobber is the shell option that protects the files from the accidental overwrites. The >> is the standard for the log files, because the append does not destroy the previous content.

This chapter covers three areas. First, why the output redirection exists โ€” the capture problem and the log problem. Second, the operators in detail โ€” the >, the >>, the 2>, the 2>>, the &>, the &>>, the 2>&1, the 1>&2, and the order of the operations. Third, the practical patterns โ€” the log capture, the error separation, the discard, the tee combination, the noclobber protection, and the here-documents. The chapter ends with a complete example session, a quick reference, best practices, common pitfalls, real-world examples, and diagrams showing the redirection order.

Key point: The > redirects the stdout and truncates the file. The >> redirects the stdout and appends. The 2> and the 2>> do the same for the stderr. The &> redirects both. The 2>&1 duplicates the stdout to the stderr, and the order matters: the > file 2>&1 sends both to the file. The set -o noclobber prevents the accidental truncation.


Why the output redirection exists

The capture problem. A command writes its output to the terminal, and the terminal is ephemeral: the output scrolls away, and the previous content is lost. To capture the output, the command’s stdout must be pointed to a file. The > operator is the mechanism. The ls > file.txt writes the listing to the file instead of the terminal, and the file can be read, searched, and archived .

The append problem. The > truncates the file, which is the right behavior when the file is a fresh capture and the wrong behavior when the file is a log. The >> appends, so the previous content is preserved. The log files are the append, and the captures are the truncate. The distinction is the difference between the > log.txt that erases the log and the >> log.txt that extends it .

The error-separation problem. The stdout is for the data, and the stderr is for the diagnostics. The > captures the data, and the 2> captures the errors. The command > out.txt 2> err.txt writes the data to the out.txt and the errors to the err.txt. The separation is the way to process the data without the noise, and the noise without the data .

The merge problem. Sometimes the two streams must be captured in one file. The &> does this in the bash, and the > file 2>&1 does it in the POSIX shell. The order matters, and the 2>&1 must come after the > file. The &> is the modern shorthand, and the > file 2>&1 is the portable form .

The discard problem. The output is not always wanted. The > /dev/null discards the stdout, and the 2> /dev/null discards the stderr. The find / -name '*.conf' 2>/dev/null silences the “Permission denied” messages, and the result is the clean list. The /dev/null is the special file that accepts the input and discards it .

The trade-off. The redirection is the shell’s job, not the program’s. The program does not know where its stdout goes; the shell sets the descriptor before the program starts. This is the power of the Unix design: the program writes to the stdout, and the shell decides whether the output goes to the terminal, the file, or the pipe. The trade-off is that the program cannot distinguish the destinations, and the buffering depends on the destination. The stdout is line-buffered to the terminal and block-buffered to the file, and the difference is the source of the delayed-output surprises in the pipelines.


a. The stdout redirection

The > operator points the stdout to a file and truncates the file. The >> appends.

ls > file.txt          # the listing to file.txt, truncate
ls >> file.txt         # the listing to file.txt, append
echo 'hello' > greeting.txt

The > creates the file if it does not exist. If the file exists, the content is replaced. The >> creates the file if it does not exist, and appends if it does.

The multiple files. The command > file1 > file2 is a mistake: the second > overrides the first, and the output goes to the file2. The > can only point the stdout to one file at a time.

The noclobber option. The set -o noclobber prevents the > from overwriting an existing file. The shell reports an error, and the file is preserved.

set -o noclobber
ls > file.txt          # error: file exists
ls >| file.txt         # the >| forces the write
set +o noclobber       # disable the option

The >| is the override, and the set +o noclobber disables the protection. The noclobber is the shell option, and the bash and the ksh support it.

The exec redirection. The exec > file points the stdout of the current shell to the file, and all the subsequent commands write to the file.

exec > log.txt
echo 'this goes to the log'
ls

The exec redirection is the way to redirect the whole script’s output. The exec 3> file opens the fd 3 for the writing, and the echo 'hello' >&3 writes to it.


b. The stderr redirection

The 2> operator points the stderr to a file and truncates. The 2>> appends.

ls /nonexistent 2> error.txt
ls /nonexistent 2>> error.txt

The 2 is the file descriptor of the stderr. The 2> is the redirect for that descriptor. The ls /nonexistent writes the “No such file or directory” to the stderr, and the 2> error.txt captures it.

The 1> and the 2>. The 1> is the explicit form of the >. The command 1> out.txt 2> err.txt is the same as the command > out.txt 2> err.txt.

command 1> out.txt 2> err.txt
command > out.txt 2> err.txt

The 2>&1 duplication. The 2>&1 copies the current target of the stdout (the fd 1) to the stderr (the fd 2). The two descriptors point to the same place.

command > file 2>&1    # both to the file
command 2>&1 > file    # the stderr to the terminal, the stdout to the file

The order is the key. The redirections are processed left to right:

  • The > file 2>&1: the stdout is pointed to the file, then the stderr is duplicated from the stdout (the file). Both go to the file.
  • The 2>&1 > file: the stderr is duplicated from the stdout (the terminal), then the stdout is pointed to the file. The stderr goes to the terminal, and the stdout goes to the file.

The 1>&2 duplication. The 1>&2 copies the stdout to the stderr. It is the way to write the error messages to the stderr in the scripts.

echo 'Error: something failed' 1>&2

The echo writes to the stdout by default, and the 1>&2 points it to the stderr. The >&2 is the shorthand for the 1>&2.


c. The combined redirection

The &> operator. The &> redirects both the stdout and the stderr to a file. The &>> appends.

command &> all.txt
command &>> all.txt

The &> is the bash shorthand for the > file 2>&1. It is the concise form, and the > file 2>&1 is the portable.

The &> and the noclobber. The &> respects the noclobber option, and the &>| forces the write.

set -o noclobber
command &> all.txt     # error
command &>| all.txt    # force

The 2>&1 | pipe. The 2>&1 | merges the stderr into the stdout and pipes both to the next command. The |& is the shorthand.

command 2>&1 | grep 'error'
command |& grep 'error'

The 2>&1 | is the portable, and the |& is the bash shorthand. The two are equivalent.

The tee combination. The tee captures the output while it flows through the pipe. The command 2>&1 | tee log.txt captures both the stdout and the stderr while displaying them.

command 2>&1 | tee build.log
command |& tee build.log

The multiple destinations. The tee file1 file2 writes to the two files and the stdout. The command | tee out.txt | grep 'error' captures the output and filters it.


Complete Example Session

# ============================================
# PART 1: THE STDOUT TRUNCATE
# ============================================

ls > file.txt
echo 'hello' > greeting.txt


# ============================================
# PART 2: THE STDOUT APPEND
# ============================================

ls >> file.txt
echo 'world' >> greeting.txt


# ============================================
# PART 3: THE STDERR REDIRECTION
# ============================================

ls /nonexistent 2> error.txt
ls /nonexistent 2>> error.txt


# ============================================
# PART 4: THE STDOUT AND THE STDERR SEPARATE
# ============================================

command > out.txt 2> err.txt


# ============================================
# PART 5: THE MERGE WITH THE ORDER
# ============================================

command > all.txt 2>&1    # both to the file
command 2>&1 > file       # the stderr to the terminal, the stdout to the file


# ============================================
# PART 6: THE &> OPERATOR
# ============================================

command &> all.txt
command &>> all.txt


# ============================================
# PART 7: THE DISCARD
# ============================================

command > /dev/null
command 2> /dev/null
command &> /dev/null


# ============================================
# PART 8: THE NO CLOBBER
# ============================================

set -o noclobber
ls > file.txt          # error: file exists
ls >| file.txt         # force
set +o noclobber


# ============================================
# PART 9: THE TEE COMBINATION
# ============================================

command 2>&1 | tee build.log
command |& tee build.log
command | tee out.txt | grep 'error'


# ============================================
# PART 10: THE LFCS PIPELINE
# ============================================

# Capture the build output while displaying it
make 2>&1 | tee build.log

# Discard the permission errors
find / -name '*.conf' 2>/dev/null

# Separate the streams
command > out.txt 2> err.txt

# The append to the log
command >> /var/log/app.log 2>&1

# The exec redirection
exec > script.log 2>&1
echo 'the script output goes to the log'
ls

# The error message to the stderr
echo 'Error: something failed' >&2

# The here-document
cat << EOF > config.txt
key1=value1
key2=value2
EOF

The ten parts show the stdout truncate, the stdout append, the stderr redirection, the separate streams, the merge with the order, the &> operator, the discard, the noclobber, the tee combination, and the LFCS pipeline.


Quick Reference

The Operators

OperatorMeaning
> fileThe stdout to the file (truncate)
>> fileThe stdout to the file (append)
1> fileThe same as the >
2> fileThe stderr to the file (truncate)
2>> fileThe stderr to the file (append)
&> fileThe both to the file (truncate)
&>> fileThe both to the file (append)
> file 2>&1The both to the file (portable)
2>&1 > fileThe stderr to the terminal, the stdout to the file
2>&1 | cmdThe both piped to the command
|& cmdThe same as the 2>&1 |
> /dev/nullThe discard the stdout
2> /dev/nullThe discard the stderr
&> /dev/nullThe discard both
1>&2The stdout to the stderr
> | fileThe force the write (the noclobber)

The File Descriptors

DescriptorStream
0The stdin
1The stdout
2The stderr
3-9The custom

The noclobber Option

CommandEffect
set -o noclobberEnable the protection
set +o noclobberDisable the protection
> fileFails if the file exists
> | fileForces the write

The Common Patterns

PatternPurpose
command > out.txt 2> err.txtThe separate streams
command > all.txt 2>&1The both to the file
command &> all.txtThe bash shorthand
command 2>/dev/nullThe discard the errors
command | tee log.txtThe capture and the display
command 2>&1 | tee log.txtThe capture the both
command >> log.txt 2>&1The append the both

Best Practices

โœ… Do This:

# Use the >> for the logs
command >> /var/log/app.log 2>&1                                          # โœ…
# Use the 2>&1 after the > for the merge
command > all.txt 2>&1                                                    # โœ…
# Use the &> for the bash shorthand
command &> all.txt                                                        # โœ…
# Use the 2>/dev/null for the discard
find / -name '*.conf' 2>/dev/null                                         # โœ…
# Use the tee for the capture and the display
command 2>&1 | tee build.log                                              # โœ…
# Use the noclobber to protect the files
set -o noclobber                                                          # โœ…
# Use the 1>&2 for the error messages in the scripts
echo 'Error' >&2                                                          # โœ…

โŒ Don’t Do This:

# Don't use the > for the logs
command > /var/log/app.log  # truncates the log                            // โŒ
# Don't write the wrong order
command 2>&1 > file  # the stderr goes to the terminal                     // โŒ
# Don't use the > for the multiple files
command > file1 > file2  # the second overrides the first                  // โŒ
# Don't forget the -0 for the xargs
find . -name '*.log' | xargs rm  # the spaces split                        // โŒ
# Don't mix the streams in the scripts
command > file.txt  # the errors are lost                                  // โŒ
# Don't rely on the buffering
command | grep 'pattern'  # the block buffering may delay                  // โŒ
# Don't forget the &>| with the noclobber
set -o noclobber; command &> file  # the error                             // โŒ

Common Pitfalls

PitfallWhy It HappensFix
The log is truncatedThe > is usedUse the >>
The errors appear on the terminalNo 2>&1Add the 2>&1
The order is wrongThe 2>&1 > fileUse the > file 2>&1
The file is overwrittenThe noclobber is offUse the set -o noclobber
The &> is not portableThe bash onlyUse the > file 2>&1
The output is mixedThe both in one fileSeparate the streams
The > fails with the noclobberThe option is onUse the `>
The delayed outputThe block bufferingUse the stdbuf or the tee

Real-World Examples

1. The Basic Truncate

ls > file.txt

2. The Append

ls >> file.txt

3. The Stderr

ls /nonexistent 2> error.txt

4. The Separate Streams

command > out.txt 2> err.txt

5. The Merge

command > all.txt 2>&1

6. The Bash Shorthand

command &> all.txt

7. The Discard

command 2>/dev/null

8. The Log Append

command >> /var/log/app.log 2>&1

9. The Tee

command 2>&1 | tee build.log

10. The Noclobber

set -o noclobber; command >| file.txt

Visual

The Redirection Order

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE REDIRECTION ORDER                                       โ”‚
โ”‚                                                              โ”‚
โ”‚  command > file 2>&1                                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  Step 1: the > file                                  โ”‚    โ”‚
โ”‚  โ”‚    The fd 1 (the stdout) โ†’ the file                  โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  Step 2: the 2>&1                                    โ”‚    โ”‚
โ”‚  โ”‚    The fd 2 (the stderr) โ†’ the fd 1 (the file)       โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  The result:                                         โ”‚    โ”‚
โ”‚  โ”‚    The stdout โ†’ the file                             โ”‚    โ”‚
โ”‚  โ”‚    The stderr โ†’ the file                             โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  command 2>&1 > file                                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  Step 1: the 2>&1                                    โ”‚    โ”‚
โ”‚  โ”‚    The fd 2 (the stderr) โ†’ the fd 1 (the terminal)   โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  Step 2: the > file                                  โ”‚    โ”‚
โ”‚  โ”‚    The fd 1 (the stdout) โ†’ the file                  โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  The result:                                         โ”‚    โ”‚
โ”‚  โ”‚    The stdout โ†’ the file                             โ”‚    โ”‚
โ”‚  โ”‚    The stderr โ†’ the terminal                         โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  The order matters because the redirections are processed    โ”‚
โ”‚  left to right.                                              โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Operators

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE OPERATORS                                               โ”‚
โ”‚                                                              โ”‚
โ”‚  command > file                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  The stdout โ†’ the file (truncate)                    โ”‚    โ”‚
โ”‚  โ”‚  The stderr โ†’ the terminal                           โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  command 2> file                                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  The stdout โ†’ the terminal                           โ”‚    โ”‚
โ”‚  โ”‚  The stderr โ†’ the file (truncate)                    โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  command &> file                                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  The stdout โ†’ the file (truncate)                    โ”‚    โ”‚
โ”‚  โ”‚  The stderr โ†’ the file (truncate)                    โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  command >> file                                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  The stdout โ†’ the file (append)                      โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  command > /dev/null 2>&1                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  The stdout โ†’ the discard                            โ”‚    โ”‚
โ”‚  โ”‚  The stderr โ†’ the discard                            โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Truncate vs the Append

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE TRUNCATE vs THE APPEND                                  โ”‚
โ”‚                                                              โ”‚
โ”‚  THE TRUNCATE (the >):                                       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  Before:                                             โ”‚    โ”‚
โ”‚  โ”‚    file.txt: line 1, line 2, line 3                  โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  command > file.txt                                  โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  After:                                              โ”‚    โ”‚
โ”‚  โ”‚    file.txt: the new output                          โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  The previous content is LOST.                       โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  THE APPEND (the >>):                                        โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  Before:                                             โ”‚    โ”‚
โ”‚  โ”‚    file.txt: line 1, line 2, line 3                  โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  command >> file.txt                                 โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  After:                                              โ”‚    โ”‚
โ”‚  โ”‚    file.txt: line 1, line 2, line 3, the new output  โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  The previous content is PRESERVED.                  โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Tee

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE TEE                                                     โ”‚
โ”‚                                                              โ”‚
โ”‚  command 2>&1 | tee build.log                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  command                                             โ”‚    โ”‚
โ”‚  โ”‚    the stdout โ”€โ”                                     โ”‚    โ”‚
โ”‚  โ”‚    the stderr โ”€โ”˜ (merged with the 2>&1)              โ”‚    โ”‚
โ”‚  โ”‚       โ”‚                                              โ”‚    โ”‚
โ”‚  โ”‚       โ–ผ                                              โ”‚    โ”‚
โ”‚  โ”‚    the pipe                                          โ”‚    โ”‚
โ”‚  โ”‚       โ”‚                                              โ”‚    โ”‚
โ”‚  โ”‚       โ–ผ                                              โ”‚    โ”‚
โ”‚  โ”‚    tee                                               โ”‚    โ”‚
โ”‚  โ”‚      โ”œโ”€โ”€โ–บ build.log (the file)                       โ”‚    โ”‚
โ”‚  โ”‚      โ””โ”€โ”€โ–บ the terminal (the stdout)                  โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  The tee is the T-splitter.                          โ”‚    โ”‚
โ”‚  โ”‚  The output is captured AND displayed.               โ”‚    โ”‚
โ”‚  โ”‚  The -a flag appends to the file.                    โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
>The stdout truncate
>>The stdout append
2>The stderr truncate
2>>The stderr append
&>The both truncate
&>>The both append
> file 2>&1The both to the file (portable)
2>&1 > fileThe stderr to the terminal
2>/dev/nullThe discard the errors
set -o noclobberThe overwrite protection
> |The force the write
1>&2The stdout to the stderr

Key takeaways:

  • The > redirects the stdout and truncates the file, and the >> appends. The 2> and the 2>> do the same for the stderr. The > creates the file if it does not exist, and the >> does the same. The append is the standard for the logs .
  • The &> redirects both the stdout and the stderr to a file. It is the bash shorthand for the > file 2>&1. The &>> appends, and the > file 2>&1 is the portable form .
  • The order of the redirections matters. The redirections are processed left to right. The > file 2>&1 sends both to the file, and the 2>&1 > file sends the stderr to the terminal and the stdout to the file .
  • The 2>&1 duplicates the stdout to the stderr. It is not a file redirection; it is the descriptor duplication. The 2>&1 must come after the > file for the both to go to the file .
  • The /dev/null discards the output. The > /dev/null discards the stdout, and the 2> /dev/null discards the stderr. The &> /dev/null discards both. The find / -name '*.conf' 2>/dev/null is the standard pattern .
  • The set -o noclobber prevents the accidental truncation. The > fails if the file exists, and the >| forces the write. The set +o noclobber disables the protection .
  • The tee captures the output while it flows through the pipe. The command 2>&1 | tee log.txt captures both the stdout and the stderr while displaying them. The -a appends, and the tee file1 file2 writes to the multiple files .
  • The 1>&2 writes the stdout to the stderr. It is the way to write the error messages to the stderr in the scripts. The echo 'Error' >&2 is the standard pattern .

Remember: The output redirection is the mechanism that controls where the stdout and the stderr go. The > and the >> redirect the stdout, the 2> and the 2>> redirect the stderr, and the &> redirects both. The 2>&1 merges the stderr into the stdout, and the order matters: the > file 2>&1 sends both to the file. The set -o noclobber protects the files, and the >| forces the write. The tee captures while it displays, and the /dev/null discards. The result is the full control over the output, which is the foundation of the shell scripting and the log management.



Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!