π Linux/Unix: I/O Streams β stdin, stdout, stderr Explained with Redirection Examples
π§² Introduction β Why Learn About I/O Streams?
In Linux/Unix, every command interacts with input and output streams: standard input (stdin), standard output (stdout), and standard error (stderr). Mastering these streams is essential for scripting, debugging, and automating workflows efficiently.
π― In this guide, youβll learn:
- What
stdin,stdout, andstderrare and how they function - How to redirect input/output using symbols like
>,2>, and< - Real-life usage for log management, silent execution, and piping
π₯ What is stdin (Standard Input)?
- File Descriptor:
0 - Function: Accepts user input from keyboard or piped data
- Default Source: Keyboard or input file
π§ͺ Example:
cat > notes.txt
Then type:
This is input from stdin.
(Press Ctrl+D to end input)
β
Input is saved into notes.txt
π€ What is stdout (Standard Output)?
- File Descriptor:
1 - Function: Sends normal output from commands to the screen
- Default Destination: Terminal
π§ͺ Example:
echo "Hello, world!"
Output:
Hello, world!
β You can redirect this output to a file:
echo "Hello" > greeting.txt
β What is stderr (Standard Error)?
- File Descriptor:
2 - Function: Outputs error messages separately from stdout
- Default Destination: Terminal
π§ͺ Example:
ls non_existing_file
Output (to stderr):
ls: cannot access 'non_existing_file': No such file or directory
β Redirect error output:
ls non_existing_file 2> error.log
π Redirection Operators β Explained
| Operator | Meaning | Example |
|---|---|---|
> | Redirect stdout (overwrite) | ls > list.txt |
>> | Append stdout | echo "new line" >> file.txt |
2> | Redirect stderr | command 2> errors.txt |
2>> | Append stderr | command 2>> errors.txt |
&> | Redirect both stdout and stderr | command &> output.log |
< | Redirect stdin from a file | wc -l < file.txt |
<< | Here-document (multi-line stdin) | cat << EOF ... EOF |
π¦ Combine Output & Error Redirection
π§ͺ Example:
ls existing.txt missing.txt > out.txt 2> err.txt
out.txtgets the listing ofexisting.txterr.txtcaptures the error formissing.txt
π§ͺ Silence Output or Errors
Discard Output:
command > /dev/null # Discard stdout
command 2> /dev/null # Discard stderr
command &> /dev/null # Discard both
β Useful for cron jobs or background tasks
π Pipe (|) vs Redirection
- Redirection (
>): Sends output to file - Pipe (
|): Sends stdout of one command into stdin of another
π§ͺ Example:
ls -l | grep ".sh"
β
Lists .sh files using ls output as input for grep
π Summary β Recap & Next Steps
Linux I/O streams provide powerful tools for managing inputs and outputs in scripts and commands. Understanding stdin, stdout, and stderr helps you filter outputs, handle errors, and build reliable automation.
π Key Takeaways:
stdin(0) = input,stdout(1) = normal output,stderr(2) = error output- Use
>,>>,2>,&>to redirect output - Combine redirections for advanced logging and debugging
- Use pipes (
|) to chain commands using output as input
β FAQs
β How can I save both output and errors into one file?
β
Use:
command &> combined.log
β What is the purpose of /dev/null?
β
It’s a black hole for output. Any data sent there is discarded.
β How do I redirect stderr to stdout?
β
Use:
command 2>&1
This merges error output into standard output.
β Can I redirect input to a command from a file?
β
Yes:
sort < names.txt
β What happens if I use > on an existing file?
β
It overwrites the file. Use >> to append instead.
Share Now :
