Skip to main content

📘 ** 0 Exit Status Code in Bash**



🎯 What You Will Learn

  1. Understand what the **exit status code **0 represents in Bash.
  2. Learn how Bash interprets success or failure based on exit codes.
  3. Detect and verify exit code 0 using $? and logical operators.
  4. Apply exit code 0 effectively in WordPress VPS scripts for automation.
  5. Identify when 0 is returned, how to test it, and how to use it in flow control.

1. 5W + 1H Framework

ElementDescription
WhatExit status 0 is a numeric signal that a command or script completed successfully.
WhyIt lets Bash (and you) decide whether to continue, stop, or perform another action automatically.
WhoBash users, WordPress VPS administrators, and DevOps engineers.
WhenEvery time a command or script completes execution.
WhereStored in the special variable $?.
HowBash automatically sets $?=0 if a command finishes without errors.

2. Concept Overview

In Bash, every command that executes returns an exit status code. This code tells the shell whether the command ran successfully or failed.

  • 0 → Success (command completed without errors)
  • Non-zero (1–255) → Failure (some form of error occurred) In logic:
  • 0** is “true”** (success condition)
  • Non-zero is “false” (failure condition)

Bash treats exit code 0 as a green light for continuing a script or chain.


3. Core Syntax and Examples

1️⃣ Check Exit Code of a Command

ls /var/www/html
echo $?

If the directory exists, $? prints 0. If it doesn’t exist, you’ll get a non-zero value like 2.

2️⃣ Use $? to Verify Success

ping -c1 google.com > /dev/null
if [ $? -eq 0 ]; then
echo "Network connection is active."
else
echo "Network check failed."
fi

When ping succeeds, exit code is 0 and the message confirms success.

3️⃣ Using true Command for Testing

true
echo $?

true is a built-in Bash command that always returns ****0, useful for testing script logic.

4️⃣ Chain Commands with &&

wp plugin update --all && echo "✅ All plugins updated successfully!"

The message prints only if the previous command exits with 0. If any error occurs, it skips the second command.

5️⃣ Manual Success Return

#!/bin/bash
echo "Script executed correctly."
exit 0

Here, exit 0 explicitly tells Bash that the script ended without errors.

6️⃣ Combined Conditional Example

tar -czf /root/backup.tar.gz /var/www/html
if [ $? -eq 0 ]; then
echo "Backup created successfully."
else
echo "Backup failed!"
fi

tar exits 0 if the archive was created properly. Any issue (e.g., missing path) would produce a non-zero code.

4. Expected Output Demonstration

CommandOutputExit CodeMeaning
ls /var/www/html; echo $?00Directory exists → success
ping -c1 google.com > /dev/null; echo $?00Network reachable
true; echo $?00Always successful
wp core is-installed --path=/var/www/html; echo $?00WordPress installation detected

5. WordPress & VPS Use Cases

ScenarioExample CommandExpected Result
Core update checkwp core update && echo "WordPress updated successfully"Confirmation message appears only when exit code is 0.
Backup verificationtar -czf /backup/site.tar.gz /var/www/html && echo "Backup OK"Prints “Backup OK” if no errors occur.
Network validation before syncping -c1 wpstrategist.com >/dev/null && rsync -av /data remote:/backuprsync only runs when ping returns 0.
Service restart sequencesystemctl restart redis && echo "Redis restarted cleanly"Prints success message when restart worked.

6. Conditional Logic Behavior

Bash operators use 0 as their logic base:

  • && → Execute next command if previous exited 0
  • || → Execute next command if previous exited non-zero
  • if → Condition passes if exit code = 0 Example:
if wp core is-installed; then
echo "WordPress detected."
fi

if executes the block only if wp core is-installed returns 0.

7. Inside a Script

#!/bin/bash
wp core is-installed --path=/var/www/html

if [ $? -eq 0 ]; then
echo "✅ WordPress detected"
exit 0
else
echo "❌ WordPress not installed"
exit 1
fi

Expected Behavior

  • If installed → prints “✅ WordPress detected” and exits 0
  • If not installed → prints “❌ WordPress not installed” and exits 1

8. Testing & Validation

Test different return cases interactively:

true; echo $? # → 0
ls /var/www/html; echo $? # → 0 if exists
wp help >/dev/null; echo $? # → 0 if wp-cli works

Each command that runs without failure will produce an exit code of 0.

9. Best Practices

  • **Always check **$? after critical operations (e.g., backups, updates).
  • Use exit 0 explicitly at the end of scripts to mark success.
  • For reliability, combine with conditional logic like && or if.
  • When automating WordPress tasks via cron, log both output and $?.
  • Treat “silence” carefully — some commands produce no output but still fail; rely on $? for certainty.

10. Troubleshooting Notes

SymptomExplanationSolution
Script always returns 0Some commands succeed even when output looks empty.Use explicit checks and logs.
Conditional always passesComparison operator mistake (= instead of -eq).Use [ $? -eq 0 ].
Automation proceeds even after failureMissing set -e or not checking $?.Add set -e or manual condition validation.

11. Quick Lab

Task

Write a script that checks if /var/www/html/wp-config.php exists, and prints a success message only when exit code is 0.

Script

#!/bin/bash
ls /var/www/html/wp-config.php >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "✅ WordPress configuration detected."
exit 0
else
echo "❌ Configuration file missing."
exit 1
fi

Expected Result

  • If file exists → ✅ WordPress configuration detected.
  • If not → ❌ Configuration file missing. Run:
echo $?

→ will return 0 if successful, 1 if not.

12. Cheat Sheet

CommandDescription
echo $?Show exit status of last command
exit 0End script successfully
trueAlways return exit code 0
command && nextExecute next command only on success
if [ $? -eq 0 ]Check if last command succeeded

13. Summary

  • Exit code 0 is the universal indicator of success in Bash.
  • $? stores this value after each command.
  • 0 acts as “true” in conditionals (if, &&).
  • Proper use of 0 improves automation reliability and script stability.
  • Always verify exit codes — never assume success from silence.