π Exit Status Code = 1 in Bash
π― What You Will Learnβ
- Understand what exit status code = 1 means in Bash.
- Learn how it differs from other exit codes like
0,2, or127. - Identify common causes and real-world scenarios where exit code
1occurs. - Learn to use
$?to check exit status in scripts. - Handle failures and automate reactions using conditional statements.
- Apply knowledge to WordPress VPS management, such as failed commands or permission errors.
1. 5W + 1H Frameworkβ
| Element | Description |
| What | Exit status code 1 indicates a general error in Bash β meaning something went wrong, but not specific enough for a distinct code (like 2, 126, 127, etc.). |
| Why | It helps scripts detect when a command fails due to syntax error, file not found, wrong arguments, or runtime error. |
| Who | Useful for system admins, DevOps engineers, and developers writing Bash scripts. |
| When | When a command or script exits unsuccessfully (not completed as expected). |
| Where | In any Linux terminal, Bash automation, or VPS script execution. |
| How | By checking the special variable $? after a command to see if it returns 1, then handle accordingly. |
2. Prerequisitesβ
Before you start, make sure you:
- Understand exit codes conceptually (0 = success, non-zero = failure).
- Know how to execute simple Bash commands.
- Know how to view exit code with
$?. - Have access to a Linux terminal or VPS environment.
3. Core Syntax and Examplesβ
π§© Basic Conceptβ
command
echo $?
If the command executes successfully β Output is 0.
If it fails β Output might be 1 (general error) or another specific code.β
Example 1 β Missing Fileβ
cat non_existing_file.txt
echo $?
Expected Output:
cat: non_existing_file.txt: No such file or directory
1
Explanation:
The cat command failed because the file doesnβt exist.
Bash returns 1 as a general error code.
Use Case (WordPress VPS):
Running cat /var/www/html/wp-config.php when file path is mistyped or missing.β
Example 2 β Invalid Argumentβ
ls --invalidoption
echo $?
Expected Output:
ls: unrecognized option '--invalidoption'
Try 'ls --help' for more information.
1
Explanation:
The ls command doesnβt recognize the flag. Bash exits with status 1.
Use Case (WordPress VPS):
When running wp plugin update --unknowflag, it will fail with exit code 1.β
Example 3 β Script Returning Exit 1β
#!/bin/bash
echo "Testing failure condition"
exit 1
Run it:
bash test_script.sh
echo $?
Expected Output:
Testing failure condition
1
Explanation: Script explicitly returns exit code 1 to indicate a general failure. Use Case: Used in Bash automation for health checks (e.g., PHP restart failed).β
Example 4 β Condition Check in Scriptβ
#!/bin/bash
ping -c 1 nonexistentsite.com &>/dev/null
if [ $? -eq 1 ]; then
echo "Ping failed. Network or DNS issue detected."
fi
Expected Output:
Ping failed. Network or DNS issue detected.
Explanation:
The ping command failed and returned exit code 1.
The script handles it with an if condition.β
Example 5 β Combining with && or ||β
mkdir /restricted && echo "Directory created" || echo "Failed with exit code $?"
Expected Output:
mkdir: cannot create directory β/restrictedβ: Permission denied
Failed with exit code 1
Explanation:
The first command failed due to lack of permission, triggering the second one with error 1.
Use Case (WordPress VPS):
When trying to write to /etc/ or /root/ without sudo.β
4. Common Scenarios That Trigger Exit Code 1β
| Scenario | Example Command | Description |
| File not found | cat missing.txt | Non-existent file path |
| Wrong argument | ls --wrong | Invalid command-line flag |
| Permission denied | mkdir /root/test | Requires sudo privileges |
| Runtime failure | grep "text" folder/ | Directory given instead of file |
| WP-CLI error | wp plugin install plugin.zip (corrupted zip) | Fails extraction |
5. Practical WordPress VPS Use Casesβ
| Use Case | Example | Explanation |
| Plugin update failed | wp plugin update all | Exit code 1 indicates error (e.g., timeout or file permission) |
| Backup script check | tar -czf backup.tar.gz /var/www/html | If compression fails, return 1 |
| Service restart check | systemctl restart mysql | If MySQL not running, code 1 |
| Deployment task | rsync /src /dest | Missing directory or permission issue triggers 1 |
6. Troubleshooting Matrixβ
| Symptom | Possible Cause | Fix |
| Exit code = 1 after running command | Command syntax error | Double-check command or flags |
| File or directory not found | Wrong path | Verify path using ls |
| Permission denied | Running as non-root | Use sudo |
| Script fails silently | No error output | Add set -e to stop on first failure |
7. Best Practicesβ
- Always check exit code using
$?after critical commands. - Use
set -ein scripts to abort automatically on error 1. - Combine with logging:
command || echo "$(date): command failed with exit code $?" >> /var/log/script.log
- Use
trapto handle unexpected exit 1 gracefully. - In production VPS, prefer meaningful return codes (e.g.,
exit 2,exit 3) for debugging clarity.
8. Quick Lab β Detecting Exit Code 1β
Step 1: Create a test scriptβ
nano /root/exit1_test.sh
Step 2: Insertβ
#!/bin/bash
echo "Checking file..."
cat /tmp/no_file_here.txt
echo "Exit code is $?"
Step 3: Runβ
bash /root/exit1_test.sh
Expected Output:β
Checking file...
cat: /tmp/no_file_here.txt: No such file or directory
Exit code is 1
9. Cheat Sheetβ
| Code | Meaning | Description |
0 | Success | Command executed successfully |
1 | General error | Non-specific failure (most common) |
2 | Misuse of shell builtins | Syntax or invalid option |
126 | Cannot execute | Permission or non-executable |
127 | Command not found | Binary or path missing |
130 | Terminated | User stopped process (Ctrl+C) |
10. Mini-Quizβ
- What does exit code
1generally mean in Bash? - What is the output of
$?if a command succeeds? - In a VPS script, how can you trigger exit code 1 manually?
- What happens if you run
mkdir /root/testas a non-root user? - How can you automatically stop a script when any command returns exit 1?
Would you like me to continue this series next with Exit Status Code = 2 (Misuse of Shell Builtins) in the same standardized format?