QA
Beginner LevelLinux Commands & SysAdmin

What are File Descriptors (0=stdin, 1=stdout, 2=stderr) and Redirection (>, >>, 2>&1)?

❓ Interview Prompt: Explain standard I/O streams (stdin, stdout, stderr), redirection operators (>, >>), and merging error streams.

In Linux, everything is treated as a file stream represented by integer File Descriptors (FD):

•
FD 0 (stdin): Standard Input (keyboard/stream input).
•
FD 1 (stdout): Standard Output (normal terminal output).
•
FD 2 (stderr): Standard Error (error diagnostic output).

Redirection Operators:

•
'> file': Overwrites stdout to a file.
•
'>> file': Appends stdout to a file.
•
'2> file': Redirects stderr to a file.
•
'2>&1': Merges stderr into stdout.

Code Implementation Example

bash
# Redirect stdout to file (overwrite)
echo "Build successful" > build.log

# Append stdout to file
echo "Timestamp: $(date)" >> build.log

# Redirect both stdout and stderr to a single log file
./deploy.sh > output.log 2>&1
💡Redirecting standard output and standard error streams in Bash.
🎯 Interview Key Takeaway

Use > to overwrite output, >> to append, and 2>&1 to merge standard error into standard output.

#File Descriptors#Redirection#stdin#stdout#stderr#Beginner
Back to all Linux Commands & SysAdmin Questions→

More Linux Commands & SysAdmin Interview Questions

Beginner LevelLinux Commands & SysAdmin

How do Linux File Permissions (chmod, chown, chgrp) work in Octal and Symbolic notation?

Beginner LevelLinux Commands & SysAdmin

What is the Linux Filesystem Hierarchy Standard (/bin, /etc, /var, /usr, /opt, /tmp)?

Beginner LevelLinux Commands & SysAdmin

How does the Linux Pipe operator (|) chain command outputs into inputs?