Skip to main content

📘 ** Command Chaining using **cmd1 ; cmd2



🎯 What You Will Learn

  1. Understand how the semicolon (;) operator works in Bash command chaining.
  2. Learn to execute multiple commands sequentially, regardless of success or failure.
  3. Differentiate between ;, &&, and || chaining behaviors.
  4. Use sequential execution in maintenance, logging, and system orchestration scripts.
  5. Apply practical WordPress VPS use cases — plugin updates, restarts, and cleanups.
  6. Identify and fix common mistakes when mixing different chaining operators.

1. 5W + 1H Framework

ElementDescription
What; is a command separator that runs multiple commands in order, regardless of whether previous ones succeed or fail.
WhyUseful for executing several independent commands in one line or script.
WhoLinux users, system administrators, and WordPress VPS managers performing sequential operations.
WhenWhen tasks don’t depend on each other’s success — e.g., cleanup, logging, or checking multiple statuses.
WhereIn shell CLI, cron jobs, or automation scripts for VPS and WordPress management.
HowCommands 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 FormulaSyntax ExampleDescription
0command1 ; command2apt update ; apt upgrade -yExecutes both commands in sequence, regardless of the first’s result.
1cmd1 ; cmd2 ; cmd3cd /var/www/html ; ls ; pwdExecutes 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.
4cmd1 ; 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 mkdir fails, 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 cd fails, ls and pwd will 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

ScenarioExample CommandDescription
1. Sequential Maintenancewp plugin update --all ; wp theme update --all ; systemctl restart lswsUpdate plugins and themes, then restart LSWS — independent execution.
2. Daily Backup Routinemysqldump -u root -p wpdb > /root/backup.sql ; gzip /root/backup.sql ; echo "Backup completed"Compress backup even if dump fails.
3. Multi-Service Restartsystemctl restart php8.3-fpm ; systemctl restart redis ; systemctl restart lswsRestart all key services sequentially.
4. Health Checkredis-cli ping ; mysqladmin ping ; systemctl is-active lswsRun independent health checks on services.
5. Log and Notifydf -h ; du -sh /var/www/html ; echo "Disk check done"Sequentially display disk usage and log message.
6. Setup & Cleanupapt install htop -y ; clear ; htopInstall and launch immediately after cleanup.

6. Best Practices

PracticeDescription
Use for independent commandsBest for tasks that do not rely on each other.
Combine with grouping ****{}For readability in multi-line scripts.
Add echo/log after eachUseful for tracing execution flow.
⚠️ Avoid for dependent tasksUse && instead for dependent command chains.
⚙️ Add logging or timestampsHelps when debugging automation scripts.
🧩 Combine with AND/OR when neededExample: cmd1 && cmd2 ; cmd3

7. Common Mistakes & Fixes

MistakeExampleProblemFix
Misusing with dependent taskswp plugin update ; systemctl restart lswsRestart runs even if update failsUse && for dependent flow
Missing spacescmd1;cmd2Works but hard to readUse cmd1 ; cmd2
Misunderstanding error flowExpecting cmd2 to skip on errorSemicolon ignores statusUse && or `
Forgetting to group{ cmd1 ; cmd2 } ; cmd3Missing braces may cause logic errorsAlways close properly

8. Troubleshooting Matrix

IssueCauseSolution
All commands run even after failureExpected conditional executionReplace ; with &&
One command fails silentlyNo error handlingAdd set -e to stop script on first error
Commands executed out of orderMisplaced semicolonsUse grouping {} to maintain sequence
Script too longMultiple ; chains inlineBreak 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:

  1. apt update → must succeed for next && command.
  2. apt upgrade -y → runs only if update succeeds.
  3. systemctl restart lsws → runs regardless (due to ;).
  4. If it fails → fallback echo "Restart failed" triggers. Expected Output:
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
...
Restart failed


11. Cheat Sheet

PatternMeaningExample
cmd1 ; cmd2Run sequentially (independent)ls ; pwd
cmd1 && cmd2Run cmd2 only if successmkdir test && echo "Done"
`cmd1cmd2`
cmd1 ; cmd2 ; cmd3Multiple sequential executionupdate ; upgrade ; clean
cmd1 && cmd2 ; cmd3Mixed chaining (dependent + sequential)apt update && apt upgrade -y ; reboot

12. Mini-Quiz

  1. What does cmd1 ; cmd2 do if cmd1 fails? It still runs cmd2 regardless of failure.
  2. What’s the difference between ; and &&? ; ignores exit status, && depends on success.
  3. How do you run three commands in order? cmd1 ; cmd2 ; cmd3
  4. What happens when you use (cmd1 ; cmd2)? Both run in a subshell, isolated from the main environment.
  5. Which operator ensures execution even if prior command fails? ;