Skip to main content

📘 ** Exit Status Code = 127 in Bash**



🎯 What You Will Learn

  1. Understand what exit status code = 127 means in Bash.
  2. Identify when Bash throws the “command not found” error.
  3. Learn the difference between exit 127, 126, and 1.
  4. Diagnose missing PATH, broken symbolic links, or non-installed binaries.
  5. Apply fixes for WordPress VPS automation (e.g., WP-CLI, PHP, MySQL not found).
  6. Learn best practices to prevent 127 errors in cron and automation scripts.

1. 5W + 1H Framework

ElementDescription
WhatExit status 127 indicates “command not found.” Bash could not locate the executable file in $PATH or the file doesn’t exist.
WhyIt helps detect when a command reference, alias, or script path is invalid or missing.
WhoVPS administrators, DevOps engineers, and Bash script authors.
WhenWhen a command, function, or script name cannot be resolved to an executable.
WhereCommon in scripts, cron jobs, or migrated VPS environments where PATHs change.
HowBy 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 $PATH variable:
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

ScenarioExampleDescription
Typo in commandehcoMisspelled command name
Missing binarywp not installedExecutable missing
Wrong file path/root/backupscript.shFile not found
Empty PATHexport PATH=""Environment variable wiped
Cron PATH issuecron runs wpCron shell lacks full PATH
Deleted alias/functionunalias wppAlias removed during session

5. WordPress VPS Use Cases

Use CaseExampleExplanation
WP-CLI command failswp plugin update --allWP-CLI not installed or PATH incorrect
PHP version mismatch/usr/bin/php8.3 -vWrong binary name or path
Cron job failurecron: wp: command not foundCron shell missing environment variables
Custom Bash script/root/scripts/wpsync.shScript path changed after migration
Redis restart aliasredisrestartAlias defined in .bashrc not loaded in cron

6. Troubleshooting Matrix

SymptomPossible CauseFix
“command not found”Binary missingInstall package with apt install <pkg>
Works manually but not in cronPATH not loadedAdd export PATH=... at top of script
Wrong directory pathScript relocatedUse absolute path
Typo or removed aliasName mismatchCheck with alias or type <command>
Script copied with CRLF line endingsBad interpreter pathRun dos2unix script.sh
WP-CLI failsNot installedReinstall using curl + chmod +x

7. Best Practices

  1. Always use absolute paths in automation:
/usr/bin/php /usr/local/bin/wp plugin list

  1. Validate PATH before running:
echo $PATH

  1. Add correct PATH to cron scripts:
export PATH=/usr/local/bin:/usr/bin:/bin

  1. Check binary locations:
which php
which wp

  1. Verify file presence:
test -f /root/backup.sh || echo "Script missing!"

  1. Keep alias and function definitions in ~/.bashrc and 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 CodeMeaningDescription
0SuccessCommand executed successfully
1General errorNon-specific failure
2Misuse of shell builtinsSyntax or structure issue
126Command cannot executePermission or binary problem
127Command not foundMissing or invalid path
130Terminated manuallyInterrupted (Ctrl+C)

10. Mini-Quiz

  1. What does exit code 127 indicate?
  2. How can you check whether WP-CLI exists in your system PATH?
  3. What’s the difference between 126 and 127?
  4. Why might a cron job return 127 even if a command works manually?
  5. Which command restores a missing default PATH variable?