π Standard Streams
π― What You Will Learnβ
- Understand the three standard streams in Bash: input, output, and error.
- Learn how Bash commands communicate through file descriptors (0, 1, 2).
- Differentiate between stdout, stderr, and stdin clearly.
- Redirect streams using operators like
>,<,2>, and&>. - Apply stream management to WordPress automation, logging, and safe error capture.
- Implement output filtering for CLI commands like
wp,tar, andgrep.
1. 5W + 1H Frameworkβ
| Element | Description |
| What | Standard streams are the default communication channels between a process and the terminal β input, output, and error. |
| Why | They allow command results, logs, and errors to be separated, redirected, or combined for automation safety. |
| Who | Bash users, WordPress admins, and DevOps engineers managing scripts and cron jobs. |
| Where | Everywhere in Linux and Bash scripting (CLI tools, system daemons, or cron jobs). |
| When | When automating WP-CLI commands, capturing logs, or redirecting data safely to files. |
| How | File descriptors 0, 1, and 2 correspond to stdin, stdout, and stderr, respectively, and can be controlled with redirection operators. |
2. Prerequisitesβ
- Completed Module 3.5 (Parameter Expansion).
- Basic familiarity with executing scripts and viewing terminal output.
- Understanding of Linux file paths and permissions.
3. Core Syntax & Concept (Revised and Verified)β
| No | Syntax Formula | Type | Syntax Example | Description | Behavior / Output |
| 1 | 0 | Standard Input (stdin) | cat 0< file.txt | Reads data into a command from a file via descriptor 0. Equivalent to cat < file.txt. | Displays contents of file.txt on screen. |
| 2 | 1 | Standard Output (stdout) | echo "Hello" 1> out.txt | Redirects output (descriptor 1) to a file instead of screen. | Creates out.txt containing βHelloβ. |
| 3 | 2 | Standard Error (stderr) | ls /root 2> error.log | Redirects error messages (descriptor 2) to error.log. | Captures permission-denied messages. |
| 4 | > | Redirect stdout (overwrite) | wp plugin list > plugins.txt | Redirects normal output to file (replaces existing). | Saves plugin list. |
| 5 | >> | Append stdout | echo "Backup done" >> log.txt | Appends output to an existing file. | Adds βBackup doneβ at end. |
| 6 | < | Redirect stdin | sort < unsorted.txt | Reads file as input instead of keyboard. | Outputs sorted content. |
| 7 | 2> | Redirect stderr | tar -czf backup.tar.gz /missing 2> err.log | Captures error messages only. | Errors go to file. |
| 8 | 2>> | Append stderr | grep TODO *.sh 2>> error.log | Appends errors (like missing files) to log. | Keeps previous logs. |
| 9 | &> | Redirect stdout + stderr | wp plugin update --all &> update.log | Combines both output and errors into one file. | Unified log. |
| 10 | 1> | Explicit stdout redirect | ls /etc 1> list.txt | Same as > β explicit descriptor version. | Creates list.txt. |
| 11 | 1>> | Explicit append stdout | date 1>> system.log | Appends timestamp to log file. | Adds line at end. |
| 12 | 2>&1 | Merge stderr β stdout | `wp core update 2>&1 | tee update.log` | Combines stderr into stdout before piping. |
| 13 | /dev/null | Null device (discard) | command > /dev/null 2>&1 | Discards both output and errors silently. | Used in cron jobs. |
| 14 | exec | Redirect whole script | exec > script.log 2>&1 | Redirects all future output to a log file. | Persistent log. |
| 15 | read var | Capture stdin into variable | read user; echo "Hi $user" | Reads user input interactively. | Displays greeting. |
| 16 | tee | Split output stream | `ls | tee files.txt` | Displays and writes output simultaneously. |
| 17 | >&2 | Manual stderr redirection | echo "Error: Invalid path" >&2 | Sends custom message to stderr. | Helpful for logging errors. |
| 18 | < input > output | Dual redirection | sort < names.txt > sorted.txt | Reads from one file, writes to another. | Sorted file created. |
4. Demonstration Exampleβ
Input Script:
#!/bin/bash
wp plugin list 1> plugins.log 2> errors.log
echo "Logs written successfully."
Expected Output:
Logs written successfully.
File Output:
plugins.logβ plugin list (stdout)errors.logβ errors (stderr) Explanation: Here,1>explicitly redirects normal output, while2>redirects errors. Bash automatically uses these file descriptors.
5. Use Cases (WordPress-VPS Focused)β
5.1 (Ref: #2) β Save Plugin List to Fileβ
wp plugin list 1> /var/log/wp_plugins.log
Use Case: Audit plugin lists automatically.β
5.2 (Ref: #3) β Capture Backup Errorsβ
tar -czf /home/wpbackup/backup.tar.gz /wrong/path 2> /var/log/backup_error.log
Output: (silent) Explanation: Errors stored separately for debugging.β
5.3 (Ref: #9) β Combine Output and Errorsβ
wp plugin update --all &> /var/log/wp_update.log
Use Case: Single log file for automation summary.β
5.4 (Ref: #13) β Quiet Cron Jobsβ
wp transient delete --expired > /dev/null 2>&1
Use Case: Suppress unwanted cron job output.β
5.5 (Ref: #12) β Log While Viewingβ
wp core update 2>&1 | tee /var/log/wp_core_update.log
Output:
Success: WordPress updated successfully.
Explanation: Displays progress and records logs simultaneously.β
5.6 (Ref: #14) β Redirect Full Script to Logβ
#!/bin/bash
exec > /var/log/wp_daily.log 2>&1
echo "Starting daily maintenance..."
wp plugin update --all
wp theme update --all
echo "Completed at $(date)"
Output (terminal): (none)
File: /var/log/wp_daily.logβ
5.7 (Ref: #17) β Manual Error Reportingβ
if [ ! -d "/var/www/html" ]; then
echo "β WordPress directory not found!" >&2
fi
Output:
β WordPress directory not found!
5.8 (Ref: #16) β Mirror Output to Logβ
wp cache flush | tee -a /var/log/wp_cache.log
Output:
Success: Object cache flushed.
6. Best Practicesβ
| Practice | Description |
Use file descriptors explicitly (1>, 2>) | Improves script readability. |
| Separate logs for stdout and stderr | Simplifies debugging. |
Always use >> for long-term logs | Avoids overwriting. |
Use /dev/null sparingly | Donβt lose critical output. |
Prefer tee for monitoring | Enables real-time feedback. |
Write intentional errors to >&2 | Distinguishes between normal and failure output. |
7. Common Mistakes & Fixesβ
| Mistake | Wrong Example | Correct Example | Explanation |
| Missing error redirection | command > log.txt | command 1> log.txt 2> err.txt | Errors remain visible. |
| Overwriting logs | > used repeatedly | Use >> | Appends logs safely. |
| Reversed merge order | 2>&1 > file | > file 2>&1 | Must redirect stdout first. |
| Unquoted paths | echo >> /path with space/file.txt | echo >> "/path with space/file.txt" | Prevents word splitting. |
Losing old logs with tee | `command | tee file` | `command |
8. Quick Lab β Unified WordPress Update Loggerβ
Script: wp_update_log.sh
#!/bin/bash
# --------------------------------------------
# Script Name : wp_update_log.sh
# Purpose : Log both output and errors during WordPress update
# --------------------------------------------
LOG="/var/log/wp_update_$(date +%F).log"
wp plugin update --all 1>> "$LOG" 2>&1
wp theme update --all 1>> "$LOG" 2>&1
echo "β
Updates logged to $LOG"
Expected Output:
β
Updates logged to /var/log/wp_update_2025-10-09.log
9. Troubleshooting Matrixβ
| Issue | Symptom | Cause | Solution |
| Log empty | Nothing recorded | Wrong redirection order | Use > file 2>&1. |
| Old logs missing | Overwritten | Use >> for append. | |
| Errors shown | stderr not redirected | Add 2> file. | |
| Cron job silent | Overused /dev/null | Test before silencing output. |
10. Static vs Dynamic Framingβ
| Framing | Description | Behavior |
| Static | Fixed file paths for logging. | Predictable but rigid. |
| Dynamic | Uses variables or timestamps. | Flexible and self-documenting. |
11. Cheat Sheetβ
| Task | Syntax / Example |
| Redirect stdin | 0< file.txt |
| Redirect stdout | 1> file.txt |
| Redirect stderr | 2> error.txt |
| Redirect both | &> all.log |
| Merge stderr β stdout | 2>&1 |
| Discard all | > /dev/null 2>&1 |
| Split output | `command |
| Global redirect | exec > logfile.txt 2>&1 |
| Manual error | echo "Error" >&2 |
12. Mini-Quizβ
| # | Question | Answer |
| 1 | Which file descriptor represents stdin? | 0 |
| 2 | Which descriptor handles normal output? | 1 |
| 3 | Which redirects errors only? | 2> |
| 4 | How to merge stdout and stderr? | 2>&1 |
| 5 | Which operator discards all output? | /dev/null |