VMware ESXi and vSphere Cluster Management

Linux Streams: Standard Input, Output, and Error

Learn how Linux stdin, stdout, and stderr work, including file descriptors, redirection, pipes, diagnostics, and common shell troubleshooting.

Linux programs communicate with their environment through streams. A stream is a data channel associated with a process for receiving input or producing output. A command may read data from a stream, write normal results to a stream, or write diagnostic messages to a separate stream.

When you run a command interactively, the streams are commonly connected to your terminal: keyboard input is read through standard input, and text appears on the terminal through standard output or standard error. The terminal is only one possible connection. A stream can instead connect to a regular file, a pipe, a device, or another process.

The three standard streams

Programs launched by a shell conventionally receive three open streams. Each stream is identified inside the process by a small integer called a file descriptor. A file descriptor is a number used by a process to identify an open input or output resource.

Stream nameAbbreviationFile descriptorTypical interactive source or destinationTypical purpose
Standard inputstdin0Terminal keyboardData read by a command
Standard outputstdout1Terminal displayNormal command results
Standard errorstderr2Terminal displayWarnings and diagnostics

Standard output and standard error can both appear on the same terminal, but they remain separate streams. This separation lets you save or pipe normal results without necessarily mixing in warnings and failure messages.

Standard input: stdin

Standard input, also called stdin, is the default source of data consumed by a program. In an interactive shell, it is commonly connected to the terminal, so a command can read what you type.

There is an important difference between shell command text and command input. The shell reads command text such as sort to decide which program to start. After the program starts, that program may read separate data from its standard input. The data typed after the command begins is input for the running program, not another shell command until the program finishes or stops reading.

Standard input can come from several sources:

  • A terminal keyboard during interactive use.
  • A regular file through input redirection.
  • A preceding command through a pipeline.
  • A here-document, which supplies a block of text.
  • A here-string, which supplies a single string of text.
sort < names.txt

Here, the shell connects names.txt to file descriptor 0 before starting sort. The command reads names from the file instead of waiting for keyboard input.

cat <<EOF
orange
apple
banana
EOF

wc -w <<< "three words here"

The first form is a here-document. The second is a here-string. Both provide data through standard input rather than requiring a separate input file.

Standard output: stdout

Standard output, or stdout, is the channel intended for a command's ordinary results. In an interactive terminal, stdout normally appears on the terminal display.

You can redirect stdout to a file with >:

ls -l > listing.txt

The shell opens listing.txt for output and connects it to file descriptor 1. Normal output goes into the file instead of appearing on the terminal. If the file already exists, > normally replaces its contents.

Use >> to append new output to the end of an existing file:

date >> activity.log

Explicit descriptor syntax makes the affected stream visible:

ls -l 1> listing.txt

1> is equivalent to > because stdout has descriptor number 1.

Standard error: stderr

Standard error, or stderr, is a separate channel for warnings, failures, and diagnostic messages. Its file descriptor is 2. In normal interactive use, stderr also points to the terminal.

Separating stderr from stdout is useful because ordinary data can be saved or passed to another command while diagnostics remain visible. For example:

grep -R "TODO" project > matches.txt 2> errors.txt
  • Matching lines written to stdout go into matches.txt.
  • Access or traversal diagnostics written to stderr go into errors.txt.

The explicit form 2> redirects stderr and replaces the destination file. The form 2>> appends diagnostics instead:

command 2> errors.txt
command 2>> errors.txt

Shell redirection syntax

Redirection is shell syntax that changes the source or destination connected to a stream. The shell performs redirections before starting the command. If the shell cannot open a requested file, the command may not run at all.

SyntaxEffectStream affectedExample
< or 0<Use a file as inputstdinsort < names.txt
> or 1>Replace a destination file with normal outputstdoutls > listing.txt
>> or 1>>Append normal output to a filestdoutdate >> activity.log
2>Replace a file with diagnosticsstderrcommand 2> errors.txt
2>>Append diagnostics to a filestderrcommand 2>> errors.txt
2>&1Send stderr where stdout currently goesstderrcommand > all.log 2>&1
|Pass stdout to the next commandstdout to stdinproducer | consumer

Discarding output with /dev/null

/dev/null is a special device that discards data written to it. Redirect stderr there when hiding diagnostics is intentional:

find / -name '*.conf' > configs.txt 2> /dev/null

This keeps normal results in configs.txt and discards diagnostic messages such as permission warnings. Suppressing stderr can hide useful information, so do not use it automatically when investigating a problem.

Duplicating a descriptor

The syntax & in a redirection target means “use another file descriptor.” Thus, 2>&1 connects stderr to the same destination that stdout currently uses. It does not mean “send stderr to a file named 1.”

command > all.log 2>&1

In this command, stdout is first redirected to all.log. Then stderr is connected to stdout's current destination, so both streams go to that file.

Redirection order matters

Shell redirections are processed from left to right. Compare these commands:

command > all.log 2>&1
command 2>&1 > all.log

In the first form, stdout is connected to all.log, and stderr is then connected to that same file. Both streams are saved there.

In the second form, stderr is first connected to the original stdout destination, usually the terminal. Then stdout is redirected to all.log. The result is that normal output goes to the file while diagnostics remain on the terminal.

Pipelines and stream flow

A pipe connects one command's standard output to another command's standard input. The pipe operator is |.

ps aux | sort -k 3 -n

ps writes its regular output into the pipe. sort receives that data as its standard input. The shell creates this connection before the commands run.

A normal pipeline transfers stdout only. Stderr is not included automatically:

producer | consumer

If producer writes diagnostics to stderr, those messages usually still appear on the terminal. To process both streams with the next command, combine them before the pipe:

command 2>&1 | tee combined.log

The shell first connects stderr to the current stdout destination. Both streams then enter the pipeline. tee displays the combined data and records it in combined.log.

Observing and testing the streams

A small shell command can write one message to stdout and another to stderr:

printf 'normal output\n'
printf 'diagnostic output\n' >&2

The first printf uses stdout. The second uses stderr because its output is redirected to descriptor 2. When run in a terminal, both messages may look identical, but they can be redirected independently:

{ printf 'normal output\n'; printf 'diagnostic output\n' >&2; } > normal.txt 2> diagnostics.txt

cat normal.txt
cat diagnostics.txt

The first cat shows the stdout message saved in normal.txt. The second shows the stderr message saved in diagnostics.txt.

Output streams are related to, but different from, a command's exit status. The exit status is a numeric result reported by the process to indicate success or failure. A command can print diagnostics and still have a particular exit status, or produce no visible output while returning a failure status.

Common problems and troubleshooting

Expected results are missing from the terminal

Check whether the command contains >, >>, or |. Output may have been written to a file or passed to a downstream command. Inspect the destination file or the next command in the pipeline.

Errors still appear after normal output was saved

Only stdout was redirected. Redirect stderr separately with 2>, or combine it with stdout using 2>&1 after the stdout redirection:

command > results.txt 2> errors.txt
command > all.log 2>&1

A log file lost previous entries

The overwrite operator > replaces existing contents. Use >> when you need to retain the file and append new output.

Errors are not included in piped processing

A normal pipeline transfers stdout only. Use 2>&1 before the pipe:

command 2>&1 | consumer

Combined output still leaves errors on the terminal

The descriptor duplication was probably applied before stdout was redirected. Use:

command > file 2>&1

Do not use command 2>&1 > file when the goal is to put both streams in the file.

Redirection fails with a permission or path error

Verify the destination path, confirm that the parent directory exists, and check write permissions. Permission failures and invalid paths are commonly reported through stderr. Because the shell performs redirection before starting the command, the command itself may never execute.

Safe usage checklist

  • Review the destination path before using > or >>.
  • Remember that > replaces an existing file, while >> appends.
  • Use separate stdout and stderr files when normal results and diagnostics need different handling.
  • Do not redirect stderr to /dev/null unless hiding the diagnostics is intentional.
  • When combining streams, place 2>&1 after the stdout redirection it should follow.
  • Check both output files and the exit status when diagnosing a command.

Summary

  • A stream is a data channel associated with a process.
  • stdin is descriptor 0 and supplies normal input.
  • stdout is descriptor 1 and carries ordinary results.
  • stderr is descriptor 2 and carries warnings and diagnostics.
  • The terminal is a common connection, but streams can also connect to files, devices, pipes, or processes.
  • < supplies input from a file, > replaces output files, and >> appends.
  • | sends stdout to the next command's stdin; stderr requires explicit redirection to join the pipeline.
  • 2>&1 duplicates stdout's current destination for stderr, and redirection order is significant.

For continued study, review Linux streams alongside shell pipelines, quoting, command exit statuses, here-documents, and process file descriptors.