📘 ** Command Chaining using **cmd1 ; cmd2
🎯 What You Will Learn
- Understand how the semicolon (
;) operator works in Bash command chaining. - Learn to execute multiple commands sequentially, regardless of success or failure.
- Differentiate between
;,&&, and||chaining behaviors. - Use sequential execution in maintenance, logging, and system orchestration scripts.
- Apply practical WordPress VPS use cases — plugin updates, restarts, and cleanups.
- Identify and fix common mistakes when mixing different chaining operators.
1. 5W + 1H Framework
| Element | Description |
| What | ; is a command separator that runs multiple commands in order, regardless of whether previous ones succeed or fail. |
| Why | Useful for executing several independent commands in one line or script. |
| Who | Linux users, system administrators, and WordPress VPS managers performing sequential operations. |
| When | When tasks don’t depend on each other’s success — e.g., cleanup, logging, or checking multiple statuses. |
| Where | In shell CLI, cron jobs, or automation scripts for VPS and WordPress management. |
| How | Commands are written in sequence separated by ; — each runs independently. |
2. Prerequisites
- Basic command-line familiarity.
- Understanding of Bash exit codes (
0= success, non-zero = failure). - Familiarity with
&&and||chaining for conditional execution.
3. Core Syntax & Concept
| No. | Syntax Formula | Syntax Example | Description |
| 0 | command1 ; command2 | apt update ; apt upgrade -y | Executes both commands in sequence, regardless of the first’s result. |
| 1 | cmd1 ; cmd2 ; cmd3 | cd /var/www/html ; ls ; pwd | Executes all three commands sequentially. |
| 2 | { cmd1 ; cmd2 ; } | { echo "Start"; date; } | Groups multiple commands to execute as a block. |
| 3 | (cmd1 ; cmd2) | (ls /etc ; ls /usr) | Runs commands in a subshell, isolated from the current environment. |
| 4 | cmd1 ; echo $? | mkdir /root/test ; echo $? | Check the exit code of the last executed command. |
4. Conceptual Flow
# Example 1
mkdir /root/test ; echo "Directory command executed"
Expected Output (if directory exists):
mkdir: cannot create directory ‘/root/test’: File exists
Directory command executed
Explanation:
- Even though
mkdirfails, the second command still runs. - Unlike
&&,;ignores success or failure.
# Example 2
cd /var/www/html ; ls ; pwd
Expected Output:
index.php
wp-content
wp-admin
wp-includes
/var/www/html
Explanation:
- Commands execute sequentially and independently.
- Even if
cdfails,lsandpwdwill still run in the current directory.
# Example 3 (WordPress Example)
wp plugin update --all ; systemctl restart lsws ; echo "✅ Maintenance done"
Expected Output (even if one fails):
Success: Updated 10 of 12 plugins.
Failed to restart lsws: Unit not found
✅ Maintenance done
Explanation:
- Each command executes in order regardless of the others’ outcome.
5. WordPress VPS Use Cases
| Scenario | Example Command | Description |
| 1. Sequential Maintenance | wp plugin update --all ; wp theme update --all ; systemctl restart lsws | Update plugins and themes, then restart LSWS — independent execution. |
| 2. Daily Backup Routine | mysqldump -u root -p wpdb > /root/backup.sql ; gzip /root/backup.sql ; echo "Backup completed" | Compress backup even if dump fails. |
| 3. Multi-Service Restart | systemctl restart php8.3-fpm ; systemctl restart redis ; systemctl restart lsws | Restart all key services sequentially. |
| 4. Health Check | redis-cli ping ; mysqladmin ping ; systemctl is-active lsws | Run independent health checks on services. |
| 5. Log and Notify | df -h ; du -sh /var/www/html ; echo "Disk check done" | Sequentially display disk usage and log message. |
| 6. Setup & Cleanup | apt install htop -y ; clear ; htop | Install and launch immediately after cleanup. |
6. Best Practices
| Practice | Description |
| ✅ Use for independent commands | Best for tasks that do not rely on each other. |
✅ Combine with grouping ****{} | For readability in multi-line scripts. |
| ✅ Add echo/log after each | Useful for tracing execution flow. |
| ⚠️ Avoid for dependent tasks | Use && instead for dependent command chains. |
| ⚙️ Add logging or timestamps | Helps when debugging automation scripts. |
| 🧩 Combine with AND/OR when needed | Example: cmd1 && cmd2 ; cmd3 |
7. Common Mistakes & Fixes
| Mistake | Example | Problem | Fix |
| Misusing with dependent tasks | wp plugin update ; systemctl restart lsws | Restart runs even if update fails | Use && for dependent flow |
| Missing spaces | cmd1;cmd2 | Works but hard to read | Use cmd1 ; cmd2 |
| Misunderstanding error flow | Expecting cmd2 to skip on error | Semicolon ignores status | Use && or ` |
| Forgetting to group | { cmd1 ; cmd2 } ; cmd3 | Missing braces may cause logic errors | Always close properly |
8. Troubleshooting Matrix
| Issue | Cause | Solution |
| All commands run even after failure | Expected conditional execution | Replace ; with && |
| One command fails silently | No error handling | Add set -e to stop script on first error |
| Commands executed out of order | Misplaced semicolons | Use grouping {} to maintain sequence |
| Script too long | Multiple ; chains inline | Break into multiline block or function |
9. Quick Lab: Practice on VPS
Objective: Perform a mini-maintenance routine.
wp plugin update --all ; systemctl restart lsws ; systemctl restart redis ; echo "✅ All tasks executed sequentially"
Expected Output:
Success: Updated 10 of 10 plugins.
✅ All tasks executed sequentially
Test Failure Scenario:
wp plugin update --path=/wrong/path ; echo "Command executed anyway"
Expected Output:
Error: This does not seem to be a WordPress installation.
Command executed anyway
10. Combined Example (Mixed Chaining)
apt update && apt upgrade -y ; systemctl restart lsws || echo "Restart failed"
Execution Order:
apt update→ must succeed for next&&command.apt upgrade -y→ runs only if update succeeds.systemctl restart lsws→ runs regardless (due to;).- If it fails → fallback
echo "Restart failed"triggers. Expected Output:
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
...
Restart failed
11. Cheat Sheet
| Pattern | Meaning | Example |
cmd1 ; cmd2 | Run sequentially (independent) | ls ; pwd |
cmd1 && cmd2 | Run cmd2 only if success | mkdir test && echo "Done" |
| `cmd1 | cmd2` | |
cmd1 ; cmd2 ; cmd3 | Multiple sequential execution | update ; upgrade ; clean |
cmd1 && cmd2 ; cmd3 | Mixed chaining (dependent + sequential) | apt update && apt upgrade -y ; reboot |
12. Mini-Quiz
- What does
cmd1 ; cmd2do ifcmd1fails? → It still runscmd2regardless of failure. - What’s the difference between
;and&&? →;ignores exit status,&&depends on success. - How do you run three commands in order?
→
cmd1 ; cmd2 ; cmd3 - What happens when you use
(cmd1 ; cmd2)? → Both run in a subshell, isolated from the main environment. - Which operator ensures execution even if prior command fails?
→
;