What are the standard streams stdin, stdout, and stderr in Linux?
Learn what stdin, stdout, and stderr are in Linux, their file descriptors, and how to redirect each stream with >, 2>, and pipes for cleaner scripts.
Expected Interview Answer
stdin, stdout, and stderr are the three standard I/O streams every Linux process opens by default: stdin (file descriptor 0) for input, stdout (fd 1) for normal output, and stderr (fd 2) for error and diagnostic output.
The shell connects stdin to the keyboard and stdout and stderr to the terminal by default, but any of them can be redirected to files, pipes, or other devices. Keeping stdout and stderr separate lets you route normal results and error messages independently, which is why you can pipe a command's output while still seeing its errors on screen. Each stream is just a file descriptor, so redirection operators like <, >, 2>, and | simply rewire where those descriptors point.
- Separates normal output from error messages
- Enables flexible redirection to files or pipes
- Allows chaining commands together with pipelines
- Makes scripts easier to log and debug
- Provides a consistent I/O model across all programs
AI Mentor Explanation
Think of a cricket match's information flow: stdin is the captain receiving instructions and signals from the dressing room, stdout is the scoreboard announcing runs and wickets to everyone, and stderr is the umpire's separate signal for no-balls and wides. Results and errors travel on different channels so spectators reading the scoreboard are never confused by the umpire's corrections, just as redirection keeps output and errors apart.
Step-by-Step Explanation
Step 1
Identify the three streams
Name stdin (fd 0), stdout (fd 1), and stderr (fd 2) and their default connections to keyboard and terminal.
Step 2
Explain stdin
Describe how programs read input from fd 0, whether from the keyboard, a file with <, or a pipe.
Step 3
Explain stdout
Show that normal program output goes to fd 1 and can be redirected with > or piped with |.
Step 4
Explain stderr
Explain that errors and diagnostics go to fd 2, kept separate so they are visible even when stdout is redirected.
Step 5
Demonstrate redirection
Use operators like >, 2>, 2>&1, and | to rewire where each descriptor points.
Step 6
Combine streams when needed
Show merging stderr into stdout with 2>&1 for unified logging, and separating them for cleaner processing.
What Interviewer Expects
- Correct file descriptor numbers (0, 1, 2)
- Understanding of default terminal connections
- Why stdout and stderr are kept separate
- Ability to redirect each stream with shell operators
- Knowledge of merging streams with 2>&1
Common Mistakes
- Mixing up the file descriptor numbers for stdout and stderr
- Thinking a plain > redirect also captures errors
- Believing stderr cannot be redirected
- Writing 2>&1 before the stdout redirect and losing errors
- Assuming pipes carry stderr by default
Best Answer (HR Friendly)
“Every Linux program automatically gets three channels: one for reading input, one for normal output, and one for error messages. Keeping the normal output and error messages on separate channels lets you save or process results while still seeing any problems.”
Code Example
# Send normal output to a file (stderr still shows on screen)
ls /tmp /missing > out.txt
# Send only errors to a file
ls /tmp /missing 2> err.txt
# Send output and errors to the same file
ls /tmp /missing > all.txt 2>&1
# Read input from a file into a command
sort < names.txt
# Discard errors entirely
rm maybe.txt 2> /dev/nullFollow-up Questions
- What does the 2>&1 redirection do and why does order matter?
- How do you redirect both stdout and stderr to /dev/null?
- What is the difference between > and >> when redirecting output?
- How does a pipe connect one command's stdout to another's stdin?
- What is a here-document and how does it feed stdin?
MCQ Practice
1. Which file descriptor number is assigned to stderr?
stdin is fd 0, stdout is fd 1, and stderr is fd 2, which is why errors are redirected with the 2> operator.
2. What does the command 'cmd > out.txt 2>&1' do?
It first points stdout to out.txt, then makes stderr follow stdout, so both streams land in the same file.
3. Where does stderr go by default in an interactive shell?
By default both stdout and stderr are connected to the terminal, so error messages appear on screen unless redirected.
Flash Cards
What are the file descriptors for the three streams? — stdin is 0, stdout is 1, and stderr is 2.
What is stdout used for? — Normal program output, connected to the terminal by default and redirectable with > or a pipe.
Why keep stderr separate from stdout? — So error and diagnostic messages stay visible even when normal output is redirected or piped elsewhere.
How do you merge stderr into stdout? — Use 2>&1 after the stdout redirect, so stderr follows wherever stdout was pointed.
How is stdin supplied from a file? — With the input redirection operator <, which points fd 0 at the file instead of the keyboard.