Understanding stdin, stdout, and stderr in Programming

By Łukasz Kallas
Picture of the author
Published on
programming image

Three standard streams: stdin, stdout, and stderr play a fundamental role in handling input and output operations.

What are stdin, stdout, and stderr?

stdin (Standard Input):
  • Description: A stream used to read input from the user or another program.
  • Common Usage: Reading user input from the terminal or receiving input data in a pipeline.
  • Default Source: Keyboard.
stdout (Standard Output):
  • Description: A stream used to write regular output data.
  • Common Usage: Displaying output to the terminal or sending data to another program in a pipeline.
  • Default Destination: Terminal.
stderr (Standard Error):
  • Description: A stream used to write error messages and diagnostics.
  • Common Usage: Displaying error messages to the terminal or logging errors.
  • Default Destination: Terminal.

Redirection

You can redirect stdin, stdout, and stderr to files or other commands in the terminal:

Redirect stdout to a file:
command > output.txt
Redirect stderr to a file:
command 2> error.txt
Redirect both stdout and stderr to a file:
command > output.txt 2>&1
Read input from a file:
command < input.txt
Redirect output from one command to another:
command | command2

Stay Tuned

Want to learn?
The best articles, links and news related to software development delivered once a week to your inbox.