🌿Linux/Unix: Environment Configuration
Estimated reading: 3 minutes 28 views

πŸ”„ 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, and stderr are 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

OperatorMeaningExample
>Redirect stdout (overwrite)ls > list.txt
>>Append stdoutecho "new line" >> file.txt
2>Redirect stderrcommand 2> errors.txt
2>>Append stderrcommand 2>> errors.txt
&>Redirect both stdout and stderrcommand &> output.log
<Redirect stdin from a filewc -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.txt gets the listing of existing.txt
  • err.txt captures the error for missing.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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: I/O Streams (stdin, stdout, stderr)

Or Copy Link

CONTENTS
Scroll to Top