Skip to main content

πŸ“˜ Command Grouping using { list; }


🎯 What You Will Learn​

  1. Understand how to use **curly braces **{} for command grouping in Bash.
  2. Learn how Bash treats grouped commands as a single logical unit.
  3. Explore differences between {} and () (current shell vs subshell).
  4. Apply grouping for combined redirection, conditional execution, and batch automation.
  5. Practice real examples with expected outputs and WordPress/VPS use cases.
  6. Avoid common syntax errors (missing semicolon, spaces, or brace placement).

1. 5W + 1H Framework​

ElementDescription
What{ list; } allows multiple commands to be grouped and executed as one block in the current shell.
WhyIt’s useful for performing several actions together β€” such as combined redirection, shared condition, or logical grouping.
WhoIdeal for system administrators, WordPress engineers, and Bash automation developers.
WhenUse it when you want several commands to share the same redirection, conditional operator, or context.
WhereCommon in scripts, aliases, and automated VPS maintenance routines.
HowEnclose 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​

NoSyntax FormulaSyntax ExampleExplanationExpected 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.txtRedirects 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​

ScenarioCommand GroupPurpose
Backup report{ tar -czf /root/backup.tar.gz /var/www; du -sh /root/backup.tar.gz; } > /root/backup.logCreate 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.txtWrite full resource report to file
Error logging{ echo "Backup failed at $(date)"; tail -n 10 /var/log/syslog; } >> /root/error.logAppend diagnostic info on failure

7. Difference Between {} and ()​

Feature{ list; }( list )
Shell TypeCurrent shellSubshell
Variable ScopeVariables persistVariables lost
Syntax{ cmd1; cmd2; }( cmd1; cmd2 )
PerformanceSlightly fasterSlightly slower
Use CaseRedirection and scriptingIsolation and testing

Example:

x=10
{ x=20; }; echo $x # Output: 20
(x=30); echo $x # Output: 20 (unchanged)


8. Best Practices​

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​

IssueCauseFix
β€œsyntax error near unexpected token }”Missing semicolon or spaceEnsure { cmd1; cmd2; } not {cmd1; cmd2;}
Group ignored redirectionRedirection placed incorrectlyPlace after closing brace, e.g. { list; } > file.txt
Variable not updatedUsed ( ) instead of { }Use curly braces for same-shell variable retention
Output missingNo echo or misused redirectionAdd explicit output command

11. Cheat Sheet​

PatternDescriptionExample
{ cmd1; cmd2; }Group commands sequentially{ ls; pwd; }
{ cmd1; cmd2; } > fileRedirect group output{ date; uptime; } > log.txt
{ cmd1; cmd2; } && cmd3Run next if group success{ wp update; wp cache flush; } && echo "OK"
`{ cmd1; cmd2; }cmd3`
`{ cmd1; cmd2; }cmd3`Pipe grouped output

12. Mini Quiz​

  1. What’s the purpose of { list; } in Bash?
  2. Does {} run commands in a subshell or current shell?
  3. What happens if you forget the semicolon before }?
  4. How would you redirect the combined output of ls and pwd into one file?
  5. Compare variable persistence between {} and () in one line.