stderr (Standard Error)
100%

Text-Fu · Lesson 3

stderr (Standard Error)

Learn how to redirect standard error separately or combine it with standard output in Bash.

Programs normally write regular results to standard output and diagnostics to a separate stream called standard error, or stderr. Keeping the streams separate lets you save useful data without mixing error messages into it.

Separating Regular Output from Errors

Consider a command whose pathname does not exist:

$ ls /fake/directory > peanuts.txt
ls: cannot access '/fake/directory': No such file or directory

The > operator redirects stdout only. The diagnostic is written to stderr, which is still connected to the terminal. Meanwhile, the shell creates or truncates peanuts.txt for stdout even though ls produces no regular result.

The standard streams conventionally use these file descriptors:

  • 0: stdin (standard input)
  • 1: stdout (standard output)
  • 2: stderr (standard error)

Why does the error from ls /missing > results.txt normally remain on the terminal?

Redirecting stderr with 2>

Put file descriptor 2 before > to redirect stderr:

$ ls /fake/directory 2> errors.txt

The shell creates or truncates errors.txt and connects it to descriptor 2. Stdout keeps its previous destination. Use 2>> errors.txt instead when error output should be appended.

Which command replaces errors.log with diagnostics from find /restricted while leaving stdout on its existing destination?

Combining stdout and stderr

To place both output streams in one file, first redirect stdout, then duplicate stderr to stdout's current destination:

$ ls /fake/directory /etc/passwd > combined.txt 2>&1

Redirections are processed from left to right:

  1. > combined.txt connects stdout to the file.
  2. 2>&1 connects stderr to wherever stdout points at that moment.

Reversing the order changes the result:

$ ls /fake/directory /etc/passwd 2>&1 > regular.txt

Here, stderr first duplicates stdout's original terminal destination. Stdout then moves to regular.txt, so the two streams end in different places.

Which Bash redirection sends both stdout and stderr from command to all.log?

Bash also provides &> as a shorter syntax for replacing a file with both streams:

$ ls /fake/directory /etc/passwd &> combined.txt

Use &>> to append both streams in Bash. The explicit > file 2>&1 form is useful to recognize because it also appears in shell scripts and documentation.

Which Bash command appends both stdout and stderr from build to build.log?

Discarding a Stream Deliberately

/dev/null is a special device that discards data written to it. Redirect stderr there only when you have determined that those diagnostics are expected and unnecessary:

$ ls /fake/directory 2> /dev/null

This does not make the command succeed or change its exit status; it only hides the diagnostic stream. During troubleshooting, preserve or display stderr instead of discarding the information you need.

What does check-data 2> /dev/null change?

To practice managing all three standard streams, try this hands-on lab:

  1. Redirecting Input and Output in Linux - In this lab, you will learn to redirect input and output in the Linux shell. You will practice controlling data flow from commands by manipulating standard output (stdout), standard error (stderr), and standard input (stdin) using operators like >, >>, 2>, and the tee command.

Lesson complete

You finished stderr (Standard Error)

You can now keep diagnostics separate or combine them with regular command output.

  • Recognize stderr as file descriptor 2.

  • Replace or append an error log with 2> or 2>>.

  • Apply multiple redirections from left to right.

  • Combine both output streams with deliberate syntax.

  • Discard diagnostics only when their loss is acceptable.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Text-Fu