Menu
Coddy logo textTech

Standard Error

Part of the Fundamentals section of Coddy's Terminal journey — lesson 41 of 82.

The third and final data stream is standard error, abbreviated as stderr. This stream is specifically for error messages and warnings, keeping them separate from normal output.

When a command fails, the error message goes to standard error, not standard output:

ls nonexistent_folder

The error message you see comes through stderr. This separation matters because you can redirect errors independently from regular output.

To redirect standard error, use 2> instead of >:

ls nonexistent_folder 2> errors.txt

The 2 refers to stderr's file descriptor number (stdout is 1, stderr is 2). This command captures the error message in errors.txt while keeping your terminal clean.

You can redirect both streams to different files:

ls existing_folder nonexistent_folder > output.txt 2> errors.txt

Successful output goes to output.txt, while any errors go to errors.txt. To send both streams to the same file, use 2>&1:

ls existing_folder nonexistent_folder > all_output.txt 2>&1

This redirects stderr to wherever stdout is going, combining everything into one file.

challenge icon

Challenge

Easy

Use the 2> operator to redirect an error message to a file. Run these two commands in order:

  1. Try to list a folder called missing_folder using ls, and redirect the error message to a file called errors.txt
  2. Use cat to display the contents of errors.txt

Hint: The format is ls foldername 2> filename. The error message goes into the file instead of appearing on screen. Use cat to verify it was captured.

Cheat sheet

The standard error stream (stderr) is used for error messages and warnings, keeping them separate from normal output.

To redirect standard error, use 2>:

ls nonexistent_folder 2> errors.txt

The 2 refers to stderr's file descriptor number (stdout is 1, stderr is 2).

You can redirect both stdout and stderr to different files:

ls existing_folder nonexistent_folder > output.txt 2> errors.txt

To send both streams to the same file, use 2>&1:

ls existing_folder nonexistent_folder > all_output.txt 2>&1

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals