stdin stdout stderr Linux: Complete Guide to Standard Streams and Redirection

August 25, 2025

Linux standard streams form the foundation of command-line communication, enabling programs to receive input and send output in a structured way. Understanding stdin (standard input), stdout (standard output), and stderr (standard error) is crucial for effective Linux system administration and shell scripting.

What Are Linux Standard Streams?

Standard streams are pre-connected input and output communication channels between a computer program and its environment. Every Linux process automatically gets three file descriptors when it starts:

  • stdin (0): Standard input stream for reading data
  • stdout (1): Standard output stream for normal program output
  • stderr (2): Standard error stream for error messages and diagnostics

These streams are implemented as file descriptors, with each having a unique numeric identifier in parentheses above.

Understanding stdin (Standard Input)

The stdin stream reads input data, typically from the keyboard or piped from another command. By default, stdin connects to the terminal, waiting for user input.

Basic stdin Examples

Here’s a simple demonstration using the cat command, which reads from stdin and writes to stdout:

$ cat
Hello World
Hello World
This is a test
This is a test
^C

In this example, cat reads each line from stdin (keyboard input) and immediately echoes it to stdout (terminal display). Press Ctrl+C to exit.

Reading from Files with stdin

You can redirect file content to stdin using the input redirection operator <:

$ cat < /etc/hostname
ubuntu-server

This redirects the contents of /etc/hostname to the cat command’s stdin.

Understanding stdout (Standard Output)

stdout carries the normal output of a program. When you run a command, its regular results appear on stdout, which typically displays on your terminal.

stdout Examples and Redirection

The echo command demonstrates basic stdout usage:

$ echo "Hello, Linux!"
Hello, Linux!

You can redirect stdout to a file using the > operator:

$ echo "Hello, Linux!" > greeting.txt
$ cat greeting.txt
Hello, Linux!

To append to an existing file instead of overwriting, use >>:

$ echo "Second line" >> greeting.txt
$ cat greeting.txt
Hello, Linux!
Second line

Understanding stderr (Standard Error)

stderr handles error messages and diagnostic information separately from normal output. This separation allows you to handle errors differently from regular program output.

stderr Examples

Let’s demonstrate stderr by trying to access a non-existent file:

$ cat nonexistent.txt
cat: nonexistent.txt: No such file or directory

The error message appears on stderr, not stdout. You can verify this by redirecting stdout to a file:

$ cat nonexistent.txt > output.txt
cat: nonexistent.txt: No such file or directory
$ cat output.txt
$ 

Notice the error still appears on the terminal (stderr) while stdout goes to the file (which remains empty).

File Descriptor Numbers

Each standard stream has a corresponding file descriptor number:

Stream File Descriptor Default Target
stdin 0 Keyboard/Terminal
stdout 1 Terminal Display
stderr 2 Terminal Display

Redirection Operators

Linux provides several operators for redirecting standard streams:

Input Redirection (<)

Redirects stdin from a file:

$ wc -l < /etc/passwd
45

Output Redirection (> and >>)

Redirect stdout to a file:

$ ls -la > directory_listing.txt
$ ls -la >> directory_listing.txt  # Append mode

Error Redirection (2> and 2>>)

Redirect stderr using the file descriptor number:

$ cat nonexistent.txt 2> error.log
$ cat error.log
cat: nonexistent.txt: No such file or directory

Advanced Redirection Techniques

Redirecting Both stdout and stderr

Redirect both streams to the same file:

$ command > output.txt 2>&1
# OR using the shorthand
$ command &> output.txt

Example with a command that produces both output and errors:

$ find /etc -name "*.conf" -type f &> search_results.txt

Separating stdout and stderr

Send stdout and stderr to different files:

$ find /etc -name "*.conf" -type f > found_files.txt 2> errors.txt

Discarding Output (/dev/null)

Discard unwanted output using /dev/null:

$ command > /dev/null          # Discard stdout
$ command 2> /dev/null         # Discard stderr
$ command &> /dev/null         # Discard both

Pipes and Standard Streams

Pipes (|) connect the stdout of one command to the stdin of another:

$ cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash

Note that pipes only connect stdout to stdin by default. stderr still goes to the terminal:

$ find /etc -name "*.conf" 2>/dev/null | head -5

Piping stderr

To pipe stderr, first redirect it to stdout:

$ command 2>&1 | grep "error"

Practical Examples and Use Cases

Log Analysis

Separate normal output from errors when analyzing logs:

$ grep -r "ERROR" /var/log/ > errors.txt 2> search_errors.txt

Silent Background Operations

Run commands silently by discarding all output:

$ rsync -av source/ destination/ &> /dev/null &

Creating Input for Commands

Use here documents to provide multiple lines of input:

$ cat << EOF > config.txt
server=localhost
port=8080
debug=true
EOF

Working with Named Pipes (FIFOs)

Create named pipes for inter-process communication:

$ mkfifo mypipe
$ echo "Hello through pipe" > mypipe &
$ cat < mypipe
Hello through pipe

Process Substitution

Use process substitution to treat command output as files:

$ diff <(ls dir1) <(ls dir2)

Debugging with Standard Streams

Use strace to monitor file descriptor operations:

$ strace -e write echo "Hello" 2>&1 | grep write

Best Practices

  • Always handle errors: Redirect stderr appropriately in scripts
  • Use meaningful redirections: Don’t blindly discard error messages
  • Test redirections: Verify your redirections work as expected
  • Document complex redirections: Comment complex redirection chains
  • Consider using tee: Use tee when you need output both on screen and in files

Using tee for Multiple Outputs

The tee command writes to both stdout and files simultaneously:

$ echo "Important message" | tee important.log
Important message

$ cat important.log
Important message

Common Mistakes and Troubleshooting

Mistake 1: Incorrect Redirection Order

# Wrong - redirects stderr to where stdout was going before redirection
$ command 2>&1 > file.txt

# Correct - redirects stdout first, then stderr to where stdout is going
$ command > file.txt 2>&1

Mistake 2: Overwriting Important Files

Use set -o noclobber to prevent accidental overwrites:

$ set -o noclobber
$ echo "test" > existing_file.txt
bash: existing_file.txt: cannot overwrite existing file

Interactive Exercise

Try this hands-on exercise to practice standard streams:

# Create test files
$ echo "Line 1" > test1.txt
$ echo "Line 2" > test2.txt

# Practice different redirections
$ cat test1.txt test2.txt missing.txt > output.txt 2> errors.txt

# Check results
$ cat output.txt
$ cat errors.txt

# Clean up
$ rm test1.txt test2.txt output.txt errors.txt

Conclusion

Mastering Linux standard streams—stdin, stdout, and stderr—is essential for effective command-line operations. These streams provide powerful ways to chain commands, handle errors gracefully, and create flexible shell scripts. By understanding redirection operators, pipes, and file descriptors, you can build sophisticated command pipelines that make your Linux workflow more efficient and robust.

Practice these concepts regularly, and you’ll find yourself naturally incorporating stream redirection into your daily Linux tasks, leading to cleaner scripts and more effective system administration.