π ** Command Chaining using **cmd1 || cmd2
π― What You Will Learnβ
- Understand the logical OR (
||) operator in Bash command chaining. - Learn how to execute the second command only if the first command fails.
- Explore practical examples with expected outputs.
- Apply OR chaining for WordPress CLI, server checks, and automation recovery.
- Learn differences between
||,&&, and;chaining for decision logic. - Implement fault-tolerant Bash scripts using conditional chaining.
1. 5W + 1H Frameworkβ
| Element | Description |
| What | The ` |
| Why | It allows you to define a fallback or recovery action when the first command doesnβt succeed β improving reliability and error handling. |
| Who | Useful for system administrators, DevOps engineers, WordPress VPS managers, and automation script developers. |
| When | Used when you want an alternative command to execute if the first one fails β such as restarting a service, running diagnostics, or logging errors. |
| Where | Commonly used in Bash scripts, CLI tasks, and WP-CLI automation loops. |
| How | The shell checks the exit status of the first command. If itβs non-zero (error), it proceeds to execute the second command. Otherwise, it skips it. |
2. Prerequisitesβ
- Basic understanding of:
- Linux shell (Bash)
- Exit status codes (
$?) - Simple command execution
- Access to a terminal (root or sudo privileges)
- WordPress CLI (for applied examples)
3. Core Syntax & Conceptβ
| No | Syntax Formula | Syntax Example | Explanation | Expected Behavior |
| 1 | `cmd1 | cmd2` | `ls /invalid/dir | |
| 2 | `(cmd1) | (cmd2)` | `(wp plugin update --all) | |
| 3 | `cmd1 arg | exit 1` | `systemctl restart nginx | |
| 4 | `cmd1 | cmd2 | ||
| 5 | `test expression | cmd2` | `[ -f /etc/passwd ] | |
| 6 | `false | true` | `false |
4. Behavioral Flow Diagramβ
+-------------+
| Run cmd1 |
+-------------+
|
v
+--------------+
| cmd1 success?|
+--------------+
| |
YES NO
| |
v v
Skip cmd2 Run cmd2
Key logic:
β If cmd1 succeeds (exit code 0), Bash skips cmd2.
β If cmd1 fails (non-zero exit code), Bash executes cmd2.β
5. Practical Examples with Expected Outputβ
Example 1 β Basic File Checkβ
cat /etc/unknown.conf || echo "File missing!"
Expected Output:
cat: /etc/unknown.conf: No such file or directory
File missing!
Explanation:
cat fails because the file doesnβt exist β triggers the fallback echo.β
Example 2 β WordPress Plugin Recoveryβ
wp plugin update --all --allow-root || wp plugin list --allow-root
Expected Output (if update fails):
Error: Could not connect to WordPress database
+----------------------+----------+
| name | status |
+----------------------+----------+
| akismet | active |
| litespeed-cache | active |
+----------------------+----------+
Use Case: Automatically lists plugins for debugging if the update process fails.β
Example 3 β Server Service Checkβ
systemctl restart redis-server || echo "Redis restart failed!"
Expected Output:
Redis restart failed!
Explanation: The message only appears if Redis cannot restart.β
Example 4 β Website Ping Fallbackβ
ping -c 2 wpstrategist.com || ping -c 2 metaxenith.com
Expected Output (if wpstrategist.com unreachable):
PING metaxenith.com (xxx.xxx.xxx.xxx): 56 data bytes
64 bytes from metaxenith.com: icmp_seq=0 ttl=57 time=35.4 ms
Explanation: If the first domain is down, the second becomes the fallback check.β
Example 5 β Automation Recoveryβ
mkdir /root/wpbackup || echo "Backup directory already exists!"
Expected Output:
mkdir: cannot create directory β/root/wpbackupβ: File exists
Backup directory already exists!
6. Use Cases in WordPress VPSβ
| Use Case | Command | Purpose |
| Plugin auto-check | `wp plugin update --all | |
| Redis check | `redis-cli ping | |
| PHP restart fallback | `systemctl restart php8.3-fpm | |
| Backup safety | `tar -czf /root/backup.tar.gz /var/www | |
| Cache recovery | `wp cache flush |
7. Comparison: && vs || vs ;β
| Operator | Execution Condition | Example | Behavior |
&& | Runs second command only if first succeeds | cmd1 && cmd2 | Conditional success chaining |
| ` | ` | Runs second command only if first fails | |
; | Runs both commands regardless | cmd1 ; cmd2 | Sequential execution |
8. Best Practicesβ
β
Use || for error handling or failover logic.
β
Always combine with meaningful messages (e.g., echo, logger).
β
When multiple fallbacks are possible, chain logically:
cmd1 || cmd2 || cmd3
β Use parentheses for complex conditions:
(cmd1 && cmd2) || (echo "Either failed")
β Avoid mixing || with && without grouping β may cause unintended order.
β Donβt rely on visual order; always think in exit codes (0 = success).β
9. Quick Labβ
Goal: Test fallback logic and verify OR chaining behavior.
# Step 1: Run this (fail intentionally)
ls /fake/dir || echo "Fallback executed."
# Step 2: Create directory and test again
mkdir /tmp/testdir
ls /tmp/testdir || echo "This will not run."
# Step 3: Test multiple fallbacks
false || echo "First fallback" || echo "Second fallback"
Expected Outputs:
Fallback executed.
# (second test - no output)
First fallback
10. Troubleshooting Matrixβ
| Problem | Cause | Solution |
| Command executes both sides | Misuse of ; instead of ` | |
| No fallback executed | First command returns success (exit 0) | Simulate failure to test |
Unclear logic mixing && and ` | ` | |
| Script continues after failure | No exit condition used | Add ` |
11. Cheat Sheetβ
| Pattern | Purpose | Example |
| `cmd1 | cmd2` | |
| `cmd1 | exit 1` | |
| `(cmd1) | (cmd2)` | |
| `cmd1 | cmd2 |
12. Mini Quizβ
- What is the main function of
||in Bash? - What happens if
cmd1succeeds incmd1 || cmd2? - Which operator executes cmd2 only when cmd1 succeeds?
- Write a command that tries to restart Redis, or echoes a warning if it fails.
- How does
cmd1 || cmd2 || cmd3behave when all commands fail?