📘 ** Exit Status Code = 127 in Bash**
🎯 What You Will Learn
- Understand what exit status code = 127 means in Bash.
- Identify when Bash throws the “command not found” error.
- Learn the difference between exit 127, 126, and 1.
- Diagnose missing PATH, broken symbolic links, or non-installed binaries.
- Apply fixes for WordPress VPS automation (e.g., WP-CLI, PHP, MySQL not found).
- Learn best practices to prevent
127errors in cron and automation scripts.
1. 5W + 1H Framework
| Element | Description |
| What | Exit status 127 indicates “command not found.” Bash could not locate the executable file in $PATH or the file doesn’t exist. |
| Why | It helps detect when a command reference, alias, or script path is invalid or missing. |
| Who | VPS administrators, DevOps engineers, and Bash script authors. |
| When | When a command, function, or script name cannot be resolved to an executable. |
| Where | Common in scripts, cron jobs, or migrated VPS environments where PATHs change. |
| How | By checking $? immediately after the failed command; value 127 confirms “command not found.” |
2. Prerequisites
You should already:
- Understand exit codes 0, 1, 2, and 126.
- Know how to check your
$PATHvariable:
echo $PATH
- Have root or user access on a Linux terminal.
3. Core Syntax and Examples
🧩 Definition
Exit code 127 = “Command not found” — Bash searched $PATH but didn’t find the executable.
Example 1 – Command Not Found
nonexistentcommand
echo $?
Expected Output:
bash: nonexistentcommand: command not found
127
Explanation:
The command doesn’t exist in any directory defined by $PATH.
Use Case (WordPress VPS):
Running wp plugin update before WP-CLI is installed → returns 127.
Example 2 – Wrong File Path
/home/user/unknownscript.sh
echo $?
Expected Output:
bash: /home/user/unknownscript.sh: No such file or directory
127
Explanation: Bash can’t find the specified script file; path is invalid. Fix: Verify file existence:
ls -l /home/user/unknownscript.sh
Example 3 – PATH Variable Missing
export PATH=""
ls
echo $?
Expected Output:
bash: ls: command not found
127
Explanation:
ls normally lives in /bin or /usr/bin. Clearing $PATH prevents Bash from finding it.
Fix:
Restore default:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Use Case:
Misconfigured cron or Docker environment missing proper $PATH.
Example 4 – Misspelled Command Name
ehco "Hello"
echo $?
Expected Output:
bash: ehco: command not found
127
Explanation:
Typographical error.
Always check spelling when you see 127.
Use Case:
Typing wpp instead of wp for WP-CLI commands.
Example 5 – Non-existent WP-CLI Path
/usr/local/bin/wp plugin list
echo $?
Expected Output:
bash: /usr/local/bin/wp: No such file or directory
127
Explanation:
Path incorrect — WP-CLI may be installed in /usr/bin/wp.
Fix:
which wp
# or reinstall
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar && mv wp-cli.phar /usr/local/bin/wp
Example 6 – Function or Alias Removed
unalias myalias
myalias
echo $?
Expected Output:
bash: myalias: command not found
127
Explanation: Alias no longer exists; Bash cannot resolve it.
4. Common Scenarios Triggering Exit Code 127
| Scenario | Example | Description |
| Typo in command | ehco | Misspelled command name |
| Missing binary | wp not installed | Executable missing |
| Wrong file path | /root/backupscript.sh | File not found |
| Empty PATH | export PATH="" | Environment variable wiped |
| Cron PATH issue | cron runs wp | Cron shell lacks full PATH |
| Deleted alias/function | unalias wpp | Alias removed during session |
5. WordPress VPS Use Cases
| Use Case | Example | Explanation |
| WP-CLI command fails | wp plugin update --all | WP-CLI not installed or PATH incorrect |
| PHP version mismatch | /usr/bin/php8.3 -v | Wrong binary name or path |
| Cron job failure | cron: wp: command not found | Cron shell missing environment variables |
| Custom Bash script | /root/scripts/wpsync.sh | Script path changed after migration |
| Redis restart alias | redisrestart | Alias defined in .bashrc not loaded in cron |
6. Troubleshooting Matrix
| Symptom | Possible Cause | Fix |
| “command not found” | Binary missing | Install package with apt install <pkg> |
| Works manually but not in cron | PATH not loaded | Add export PATH=... at top of script |
| Wrong directory path | Script relocated | Use absolute path |
| Typo or removed alias | Name mismatch | Check with alias or type <command> |
| Script copied with CRLF line endings | Bad interpreter path | Run dos2unix script.sh |
| WP-CLI fails | Not installed | Reinstall using curl + chmod +x |
7. Best Practices
- Always use absolute paths in automation:
/usr/bin/php /usr/local/bin/wp plugin list
- Validate PATH before running:
echo $PATH
- Add correct PATH to cron scripts:
export PATH=/usr/local/bin:/usr/bin:/bin
- Check binary locations:
which php
which wp
- Verify file presence:
test -f /root/backup.sh || echo "Script missing!"
- Keep alias and function definitions in
~/.bashrcand reload using:
source ~/.bashrc
8. Quick Lab – Missing Command Simulation
Step 1: Run a non-existent command
idontexist
echo $?
Expected Output:
bash: idontexist: command not found
127
Step 2: Create and run a valid script
echo 'echo "Command works!"' > /root/testcmd.sh
chmod +x /root/testcmd.sh
/root/testcmd.sh
echo $?
Expected Output:
Command works!
0
Step 3: Rename it and try again
mv /root/testcmd.sh /root/testcmd2.sh
/root/testcmd.sh
echo $?
Expected Output:
bash: /root/testcmd.sh: No such file or directory
127
9. Cheat Sheet
| Exit Code | Meaning | Description |
0 | Success | Command executed successfully |
1 | General error | Non-specific failure |
2 | Misuse of shell builtins | Syntax or structure issue |
126 | Command cannot execute | Permission or binary problem |
127 | Command not found | Missing or invalid path |
130 | Terminated manually | Interrupted (Ctrl+C) |
10. Mini-Quiz
- What does exit code
127indicate? - How can you check whether WP-CLI exists in your system PATH?
- What’s the difference between 126 and 127?
- Why might a cron job return 127 even if a command works manually?
- Which command restores a missing default PATH variable?