πŸ”§ 4. Bash Functions & Scripting
Estimated reading: 3 minutes 38 views

πŸ”„ Bash: I/O Redirection in Scripts – Use >, <, >>, 2> and More


🧲 Introduction to Bash I/O Redirection – Manage Script Input and Output Efficiently

In Bash scripting, I/O redirection is used to control how input and output flows between commands, files, and streams. It allows you to:

  • Redirect command output to files
  • Read input from files
  • Capture error messages
  • Combine outputs for logging or debugging

Using redirection makes your scripts cleaner, more powerful, and automation-ready.


🎯 In this article, you’ll learn:

  • How to use >, >>, <, 2>, &>, and /dev/null
  • Redirect stdout, stderr, and input streams
  • Examples of output logging and error handling
  • Best practices for safe file redirection

πŸ“€ Output Redirection Operators

OperatorDescriptionExample
>Redirect standard output (overwrite)echo "Hello" > file.txt
>>Redirect standard output (append)echo "Add" >> file.txt
2>Redirect standard errorls file 2> error.log
&>Redirect both stdout and stderrcommand &> output.log
2>&1Redirect stderr to stdoutcommand > out.txt 2>&1

πŸ§ͺ Example: Overwrite Output to File

echo "New log starts here" > log.txt

βœ… Replaces contents of log.txt.


πŸ§ͺ Example: Append Output to File

echo "Another line" >> log.txt

βœ… Adds to the end of log.txt.


πŸ§ͺ Example: Redirect Errors Only

ls /no/such/file 2> errors.log

βœ… Captures only the error message.


πŸ§ͺ Example: Combine Output and Errors

cp source.txt destination/ &> all_output.log

βœ… Captures both success messages and errors.


πŸ“₯ Input Redirection Operator

OperatorDescriptionExample
<Redirect input from fileread line < input.txt

πŸ§ͺ Example: Read from File

while read line; do
  echo "Line: $line"
done < names.txt

βœ… Reads each line from names.txt.


πŸ”• Suppress Output Using /dev/null

To discard output (like for silent background jobs), redirect it to /dev/null:

command > /dev/null 2>&1

βœ… This silences both output and errors.


πŸ”„ Advanced Use: Redirect Inside Scripts

#!/bin/bash

logfile="script.log"

echo "Script started" > "$logfile"
echo "Performing task..." >> "$logfile"

cp /path/does/not/exist /tmp 2>> "$logfile"
echo "Script completed" >> "$logfile"

βœ… This logs all actions and appends any error messages.


πŸ“Œ Summary: Bash I/O Redirection

I/O redirection in Bash scripting gives you complete control over input, output, and error streams, making your scripts more professional and production-ready. Use >, >>, <, 2>, and &> to capture, silence, or reroute data streams.

πŸ” Key Takeaways:

  • Use > to overwrite and >> to append output to files
  • Use 2> to capture errors
  • Use < to feed input from files
  • Combine output and errors with &> or 2>&1
  • Silence all output with /dev/null

βš™οΈ Real-world Uses:

  • Logging script activity and errors
  • Reading config files line-by-line
  • Running silent cron jobs or batch processes

❓ FAQ – Bash I/O Redirection


❓ What’s the difference between > and >>?
βœ… > overwrites the file, while >> appends to it.


❓ How do I redirect only error messages?
βœ… Use 2>:

command 2> error.log

❓ How can I silence all output from a command?
βœ… Use:

command > /dev/null 2>&1

❓ How do I read input from a file in a script?
βœ… Use input redirection:

while read line; do echo "$line"; done < file.txt

❓ What is 2>&1 used for?
βœ… It redirects stderr (2) to the same place as stdout (1), so both can be captured together.


Share Now :

Leave a Reply

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

Share

🟒 Bash: IO Redirection in Scripts (>, <, >>, 2>)

Or Copy Link

CONTENTS
Scroll to Top