π 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
| Operator | Description | Example |
|---|---|---|
> | Redirect standard output (overwrite) | echo "Hello" > file.txt |
>> | Redirect standard output (append) | echo "Add" >> file.txt |
2> | Redirect standard error | ls file 2> error.log |
&> | Redirect both stdout and stderr | command &> output.log |
2>&1 | Redirect stderr to stdout | command > 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
| Operator | Description | Example |
|---|---|---|
< | Redirect input from file | read 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
&>or2>&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 :
