π Command Grouping Using cmd1 |& cmd2 (Pipe stdout and stderr)
1. What You Will Learnβ
- Understand how the
|&operator works in Bash to send both stdout and stderr to another command. - Learn how it differs from the regular pipe (
|) which only sends stdout. - Apply
|&to handle error logs and outputs together in pipelines. - Use
|&for debugging WordPress scripts, service commands, or complex CLI loops. - Learn best practices and compatibility notes for using
|&in Bash 4+ environments. - Recognize practical examples combining
|&withgrep,tee, orawk.
2. 5W + 1H Frameworkβ
| Element | Description |
| What | The ` |
| Why | To capture and process all messagesβboth normal and errorβin a single unified stream, especially useful in automation and debugging. |
| Who | Ideal for system administrators, DevOps engineers, and WordPress VPS managers who need to analyze combined command output for troubleshooting. |
| Where | Commonly used in Bash scripts, cron jobs, and diagnostic automation on Linux servers. |
| When | Used when you need to process or log both successful and error messages through the same pipeline. |
| How | By joining commands using ` |
3. Core Syntax & Conceptβ
The |&** operator** is a Bash shortcut that sends both stdout and stderr from the first command into the stdin of the second command.
It behaves like a regular pipe (|) but merges error messages into the same stream.
3.1 Basic Syntaxβ
cmd1 |& cmd2
Equivalent to:
cmd1 2>&1 | cmd2
Explanation:
stdout(file descriptor 1) andstderr(file descriptor 2) fromcmd1are both redirected into the pipeline.cmd2receives all combined output.
3.2 Example 1 β Capturing Errors and Output Togetherβ
ls /nonexistent /etc |& grep "No such"
Explanation:
The command ls /nonexistent /etc produces an error message because the first directory does not exist.
Using |& ensures that both standard and error outputs are passed to grep.
Expected Output:
ls: cannot access '/nonexistent': No such file or directory
Use Case: Quickly filter and view specific error messages from a command without losing them.β
3.3 Example 2 β Debugging a WordPress Commandβ
wp plugin update --all --allow-root |& tee /root/wp-update.log
Explanation:
wp plugin update --allupdates all plugins.- Both successful updates and error messages are piped to
tee, which saves the output to/root/wp-update.logand displays it on screen. Expected Output (Partial):
Updating Akismet (4.2.1 β 4.2.2)
Success: Updated 1 of 1 plugins.
Warning: Unable to update litespeed-cache (connection timeout)
Use Case: Debugging automated WordPress maintenance scripts that occasionally produce network or timeout errors.β
3.4 Example 3 β Merging Logs for Analysisβ
( wp plugin list --allow-root; wp theme list --allow-root ) |& grep active
Explanation:
This runs two WordPress CLI commands, merging their outputs and errors into a single stream.
grep active filters only the active items.
Expected Output:
akismet active
litespeed-cache active
twentytwentyfour active
Use Case: Analyze multiple types of WordPress data (plugin/theme) with unified output and consistent formatting.β
3.5 Example 4 β Checking System Resource Errorsβ
df -h /mnt |& grep "No such"
Explanation:
If /mnt is unavailable, the error message is piped directly to grep.
No need for separate redirection (2>&1).
Expected Output:
df: /mnt: No such file or directory
Use Case: Simplify file system checks or scheduled cron validation.β
3.6 Concept Summaryβ
- The pipe
|passes stdout only. - The pipe
|&passes stdout + stderr together. |&is shorthand for2>&1 |.- It works only in Bash 4.0+, not POSIX
sh. - Very effective for debugging, automation logs, and combined reporting.
- Ideal when working with unpredictable scripts that may produce errors (e.g., during WordPress updates, database imports, or network operations).
4. Practical Use Casesβ
| Use Case | Command Example | Expected Output |
| Capture all plugin update logs | `wp plugin update --all | & tee /root/wp.log` |
| Check missing PHP extensions | `php -m | & grep -i missing` |
| Log MySQL import output | `mysql -u root -p dbname < file.sql | & tee import.log` |
| Monitor Redis status | `redis-cli info | & grep connected_clients` |
| Test connection to WP-CLI | `wp core is-installed --allow-root | & grep Error` |
5. Benefitsβ
| Category | Description |
| Unified Output | Combines stdout and stderr for full visibility. |
| Easier Debugging | Helps diagnose issues without losing error messages. |
| Clean Logging | Single output stream simplifies automation logs. |
| Efficiency | Avoids creating separate redirection lines like 2>&1. |
| Compatibility | Cleaner Bash syntax for advanced users. |
6. Implementation Stepsβ
- Identify a command that may generate both output and error messages.
- Replace the regular pipe
|with|&. - Pipe the result into a processor like
grep,awk, ortee. - Run the command and verify that both stdout and stderr appear.
- Optionally, save or filter the combined output as needed.
7. Best Practicesβ
| Practice | Description |
| Always use ` | &` for mixed-output scripts |
Use tee to log output and still see it live | Example: `cmd |
| Combine with timestamped logs | Example: `cmd |
| Avoid unnecessary piping | Use ` |
| Validate Bash version | Works on Bash 4.0+ (run bash --version to confirm). |
8. Static vs Dynamic Framing (WordPress Context)β
| Type | Example | Behavior |
| Static | `ls /nonexistent | & grep "No such"` |
| Dynamic | `wp plugin update --all | & tee update.log` |
9. Go-Live Checklistβ
- Confirm your Bash version supports
|&(4.0 or higher). - Test your command manually before embedding in cron or scripts.
- Use
teefor persistent logs when running automation. - Include error checks (
grep Error) for validation in pipelines. - If backward compatibility is needed, use
2>&1 |instead.
10. Troubleshooting Matrixβ
| Symptom | Possible Cause | Solution |
| βcommand not foundβ error | Using ` | &` in a non-Bash shell |
| Missing error messages | Command outputs only to stderr | Confirm ` |
| Empty log file | Command produced no output | Verify command actually generates stdout or stderr. |
| Broken pipeline | Command terminates early | Test each stage independently. |
11. Quick Labβ
Task: Capture both normal and error outputs when checking all WordPress sites for broken configurations.
for site in /home/*/*/public_html; do
echo "Checking: $site"
( wp core is-installed --path="$site" --allow-root; wp plugin list --allow-root --path="$site" ) |& tee -a /root/wp-check.log
echo "-----------------------------------"
done
Expected Output (Partial):
Checking: /home/metaxenith/public_html
Success: WordPress is installed.
akismet active
litespeed-cache active
-----------------------------------
Checking: /home/devwp/public_html
Error: This does not seem to be a WordPress installation.
-----------------------------------
12. Cheat Sheetβ
| Operator | Meaning | Example |
| ` | ` | Pipe only stdout |
| ` | &` | Pipe stdout and stderr |
| `2>&1 | ` | Traditional equivalent of ` |
| ` | & tee file` | Log all outputs |
| ` | & grep` | Filter combined output |
13. Mini-Quizβ
| # | Question | Answer |
| 1 | What is the purpose of ` | &` in Bash? |
| 2 | What is `cmd1 | & cmd2` equivalent to? |
| 3 | Does ` | &work in POSIXsh`? |
| 4 | How do you log and view output simultaneously? | Use `cmd |
| 5 | Why is ` | &` useful for automation? |
Would you like the next topic to continue with
Module 2.1.f β Combining Multiple Pipes (cmd1 | cmd2 |& cmd3), which merges selective stderr redirection within longer command pipelines?