π Command Grouping using { list; }
π― What You Will Learnβ
- Understand how to use **curly braces **
{}for command grouping in Bash. - Learn how Bash treats grouped commands as a single logical unit.
- Explore differences between
{}and()(current shell vs subshell). - Apply grouping for combined redirection, conditional execution, and batch automation.
- Practice real examples with expected outputs and WordPress/VPS use cases.
- Avoid common syntax errors (missing semicolon, spaces, or brace placement).
1. 5W + 1H Frameworkβ
| Element | Description |
| What | { list; } allows multiple commands to be grouped and executed as one block in the current shell. |
| Why | Itβs useful for performing several actions together β such as combined redirection, shared condition, or logical grouping. |
| Who | Ideal for system administrators, WordPress engineers, and Bash automation developers. |
| When | Use it when you want several commands to share the same redirection, conditional operator, or context. |
| Where | Common in scripts, aliases, and automated VPS maintenance routines. |
| How | Enclose commands inside { } separated by semicolons (;), and **add a space after **{ and before ****}. Example: { cmd1; cmd2; }. |
2. Prerequisitesβ
- Familiarity with:
- Bash terminal and basic command execution
- Command chaining (
&&,||,;) - Exit status codes (
$?) - Sudo/root access for VPS or WordPress CLI usage
3. Core Syntax & Conceptβ
| No | Syntax Formula | Syntax Example | Explanation | Expected Behavior |
| 1 | { list; } | { cmd1; cmd2; } | Groups multiple commands into a single block executed in current shell. | Executes both sequentially. |
| 2 | { cmd1; cmd2; } > file.txt | { ls; pwd; } > result.txt | Redirects entire block output to one file. | Both outputs go to result.txt. |
| 3 | { cmd1; cmd2; } && cmd3 | { mkdir backup; cp file.txt backup/; } && echo "Success!" | Executes group; only runs next command if group succeeds. | Echo runs after both succeed. |
| 4 | `{ cmd1; cmd2; } | cmd3` | `{ cp wrong.txt /root/; echo "Done"; } | |
| 5 | `{ cmd1; cmd2; } | cmd3` | `{ cat file1; cat file2; } | grep "error"` |
| 6 | { cmd1; cmd2; cmd3; } | { echo "Start"; date; uptime; } | All commands run sequentially within same environment. | Prints sequence results in order. |
4. Behavior & Execution Flowβ
Start
β
Enter { } block
ββ Run cmd1
ββ Run cmd2
ββ Run cmd3
β
Exit block (return last commandβs status)
Note:
{}runs commands in the same shell β variables remain accessible afterward.- Must include **space after **
{and semicolon before ****}, e.g.{ cmd; }.
5. Practical Examples with Expected Outputβ
Example 1 β Basic Group Executionβ
{ echo "Hello"; echo "World"; }
Expected Output:
Hello
World
Explanation: Both commands execute as one logical unit in the same shell.β
Example 2 β Group Redirectionβ
{ echo "System Info:"; uname -a; } > sysinfo.txt
cat sysinfo.txt
Expected Output:
System Info:
Linux GC-SG-M16GB 6.8.0-45-generic #45-Ubuntu SMP x86_64 GNU/Linux
Use Case: Saves system info from grouped commands into one file.β
Example 3 β WordPress Plugin Status Summaryβ
{ wp plugin list --allow-root; wp theme list --allow-root; } > /root/wp-summary.txt
Expected Output:
# (inside /root/wp-summary.txt)
+-------------------+----------+
| name | status |
+-------------------+----------+
| akismet | active |
| litespeed-cache | active |
...
+-------------------+----------+
| theme | status |
| twentytwentyfive | active |
Explanation: Both plugin and theme lists stored together in one report file.β
Example 4 β Combined Condition and Notificationβ
{ systemctl restart php8.3-fpm; systemctl restart redis-server; } && echo "Services restarted"
Expected Output:
Services restarted
Explanation: Both restarts must succeed for message to appear.β
Example 5 β Fallback Recovery Exampleβ
{ systemctl restart nginx; systemctl reload nginx; } || echo "Nginx failed to start"
Expected Output:
Nginx failed to start
Explanation: If both commands inside block fail, the fallback echo executes.β
6. WordPress VPS Use Casesβ
| Scenario | Command Group | Purpose |
| Backup report | { tar -czf /root/backup.tar.gz /var/www; du -sh /root/backup.tar.gz; } > /root/backup.log | Create backup and log size together |
| Cache flush routine | { wp cache flush; redis-cli flushall; } && echo "All cache cleared" | Combine WP + Redis clearing |
| Restart routine | { systemctl restart lsws; systemctl restart php8.3-fpm; } | Restart stack services in one action |
| Auto audit | { df -h; free -m; uptime; } > /root/server-audit.txt | Write full resource report to file |
| Error logging | { echo "Backup failed at $(date)"; tail -n 10 /var/log/syslog; } >> /root/error.log | Append diagnostic info on failure |
7. Difference Between {} and ()β
| Feature | { list; } | ( list ) |
| Shell Type | Current shell | Subshell |
| Variable Scope | Variables persist | Variables lost |
| Syntax | { cmd1; cmd2; } | ( cmd1; cmd2 ) |
| Performance | Slightly faster | Slightly slower |
| Use Case | Redirection and scripting | Isolation and testing |
Example:
x=10
{ x=20; }; echo $x # Output: 20
(x=30); echo $x # Output: 20 (unchanged)
8. Best Practicesβ
β
Always include semicolon (;) before } β mandatory.
β
Leave **space after **{ and before }.
β
Use {} for commands that need to share environment or variables.
β
Combine with &&, ||, or redirection for more control.
β
Group related operations together for readability.
β Donβt use {} for complex isolated operations β use ( ) instead.β
9. Quick Labβ
Goal: Run multiple system checks and log to a file.
{ echo "===== Server Health ====="; date; uptime; df -h; free -m; } > /root/health-report.txt
cat /root/health-report.txt
Expected Output:
===== Server Health =====
Fri Oct 10 21:52:36 UTC 2025
21:52:36 up 12 days, 3:15, 2 users, load average: 0.05, 0.10, 0.20
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 60G 22G 35G 39% /
Mem: 16000 8200 7800
10. Troubleshooting Matrixβ
| Issue | Cause | Fix |
βsyntax error near unexpected token }β | Missing semicolon or space | Ensure { cmd1; cmd2; } not {cmd1; cmd2;} |
| Group ignored redirection | Redirection placed incorrectly | Place after closing brace, e.g. { list; } > file.txt |
| Variable not updated | Used ( ) instead of { } | Use curly braces for same-shell variable retention |
| Output missing | No echo or misused redirection | Add explicit output command |
11. Cheat Sheetβ
| Pattern | Description | Example |
{ cmd1; cmd2; } | Group commands sequentially | { ls; pwd; } |
{ cmd1; cmd2; } > file | Redirect group output | { date; uptime; } > log.txt |
{ cmd1; cmd2; } && cmd3 | Run next if group success | { wp update; wp cache flush; } && echo "OK" |
| `{ cmd1; cmd2; } | cmd3` | |
| `{ cmd1; cmd2; } | cmd3` | Pipe grouped output |
12. Mini Quizβ
- Whatβs the purpose of
{ list; }in Bash? - Does
{}run commands in a subshell or current shell? - What happens if you forget the semicolon before
}? - How would you redirect the combined output of
lsandpwdinto one file? - Compare variable persistence between
{}and()in one line.