Skip to content
TaeyoungKim.dev

Linux stdout vs. stderr: Save Normal Output and Errors Separately

LinuxWritten 3 min readTaeyoungKim
LinkedInX

You save a command's output with >, but an error message still appears in the terminal. The redirection may have worked: normal output and errors travel through different streams. In the shell, standard output (stdout) is file descriptor 1 and standard error (stderr) is file descriptor 2. Knowing which stream a message uses tells you what entered the log file.

Which output does > send to a file?

The diagram directs stdout 1 to result.txt with > and stderr 2 to error.txt with 2>. Each stream can have its own destination; by default, > redirects only stdout.

This standalone example prints one normal message and one error message. >&2 sends the second printf to the error stream.

bash
{ printf 'Normal result\n'; printf 'Error message\n' >&2; } > result.txt

result.txt contains only Normal result, while Error message appears in the terminal. That is because > changes the destination of stdout 1 by default. Running the same command against an important existing file can overwrite it, so use a fresh filename for a test. Appending requires >>, but first check which stream you intend to save.

Use 2> to redirect stderr separately

Set a destination for each stream when you want to keep errors in another file.

bash
{ printf 'Normal result\n'; printf 'Error message\n' >&2; } \
  > result.txt 2> error.txt

Now result.txt contains the normal message and error.txt contains the error message. The 2 in 2> names stderr. Shells can also combine both streams in one file, but mixing or hiding errors can make diagnosis harder. Start by keeping them separate.

Do not judge success only by where output appeared. Check the exit status too. A program may print an error message but return status 0, or end with a nonzero status while printing little or nothing. A quiet automation script is not necessarily a successful one.

What to check when a file is empty or an error disappears

First, use a small example to see which stream the command writes to. Then check whether the redirection applies to one command or to a group of commands in braces. When you use a pipe, stdout is the stream passed to the next command by default, so seeing stderr separately is normal.

Be careful with 2>/dev/null while investigating a problem; it discards errors. In production automation, keep errors in logs with suitable access controls and retention instead. Error text may include secrets, so inspect it before sharing or uploading it.

Key takeaways: read normal output and errors separately

> redirects stdout by default; 2> redirects stderr. An error appearing on screen rather than in the file does not mean the command's whole output failed to save. Inspect each stream's contents alongside the exit status to locate failures faster.

Author

TaeyoungKim

Connecting technical foundations with implementation, verification, and production decisions.

#Linux#Bash#stdout#stderr#redirection

Read next