What are STDOUT and STDERR?¶
In Unix-like operating systems, every process or command generates three standard streams for input and output:
STDOUT (Standard Output):¶
This is where a program sends its normal output (results, responses, or information that it needs to display).
STDERR (Standard Error):¶
This is where a program sends its error messages (warnings, errors, or issues it encounters).
Both streams are represented as file descriptors in the background: - STDOUT is file descriptor 1 - STDERR is file descriptor 2
By default, both streams are displayed on the terminal.
Example 1: STDOUT (Normal Output)¶
Let's look at a simple example of a command that generates normal output (STDOUT):
echo "Hello, World!"
Output (STDOUT):
Hello, World!
In this case, the command echo outputs the text Hello, World! to the terminal. This is STDOUT because it’s the normal, expected output of the command.
Example 2: STDERR (Error Output)¶
Now, let's look at an example of a command that generates an error (STDERR):
ls non_existent_directory
Output (STDERR):
ls: cannot access 'non_existent_directory': No such file or directory
Here, the ls command is trying to list a directory that doesn’t exist. The error message cannot access 'non_existent_directory': No such file or directory is printed to STDERR because it’s an error, not normal output.
Redirecting STDOUT and STDERR¶
You can redirect these outputs to different places, like a file or /dev/null (to discard the output). Here's how you can redirect each:
1. Redirect STDOUT (Normal Output):¶
If you only want to redirect the normal output (STDOUT) to a file:
echo "Hello, World!" > output.txt
Result: The output.txt file will contain:
Hello, World!
But, if there was an error generated in this case (e.g., trying to write to a file you don't have permission to), the error would still be shown on the terminal because we haven’t redirected STDERR.
2. Redirect STDERR (Error Output):¶
To redirect the error output (STDERR) to a file:
ls non_existent_directory 2> error.txt
Result: The error.txt file will contain:
ls: cannot access 'non_existent_directory': No such file or directory
But the normal output (if any) will still be printed on the terminal.
3. Redirect Both STDOUT and STDERR:¶
You can redirect both STDOUT and STDERR to the same place (e.g., a file or /dev/null):
To redirect both to a file:
ls non_existent_directory > output.txt 2>&1
This will redirect both normal output and error messages to output.txt.
To discard both STDOUT and STDERR (suppress everything):
ls non_existent_directory > /dev/null 2>&1
This will suppress all output and errors, so nothing is printed to the terminal.
Visualizing STDOUT and STDERR:¶
Here’s a visualization of what happens when you run a command in a terminal:
Normal Output (STDOUT):¶
$ echo "Hello, World!"
Hello, World! <-- This is STDOUT
Error Output (STDERR):¶
$ ls non_existent_directory
ls: cannot access 'non_existent_directory': No such file or directory <-- This is STDERR
Summary:¶
- STDOUT is the normal output stream that contains the expected result of a command.
- STDERR is the error output stream that contains error messages when things go wrong.
- By redirecting these streams, you can control where the output goes (file, terminal, etc.) and decide which type of output you want to suppress or capture.