📘 Exit Status Code = 126 in Bash
🎯 What You Will Learn
- Understand the meaning and root cause of exit status code = 126 in Bash.
- Learn how it differs from other execution-related errors like
127and1. - Identify situations when a command exists but cannot execute (permissions, binary issues).
- Debug permission and execution errors using CLI tools (
ls -l,chmod,file). - Apply solutions in WordPress VPS contexts, especially with cron jobs, scripts, and WP-CLI automation.
- Learn practical fixes and preventive strategies to avoid error 126 in production.
1. 5W + 1H Framework
| Element | Description |
| What | Exit status 126 means the command was found but cannot execute — often due to missing execute permissions, wrong binary format, or restricted shell policy. |
| Why | It signals Bash located the command or script but OS denied execution (permissions, architecture, or security context). |
| Who | System administrators, Bash developers, and DevOps managing executable scripts. |
| When | When running a script or binary file without +x permission, or using incorrect interpreter path. |
| Where | Common in VPS cron jobs, automation scripts, plugin updates, or any .sh file execution. |
| How | Check with $? immediately after a failed command — value 126 confirms "cannot execute" condition. |
2. Prerequisites
You should already:
- Understand file permissions and the meaning of
x(execute). - Know how to use commands:
chmod,ls -l,cat, andfile. - Have a test VPS or Linux environment with a writable
/rootor/homedirectory.
3. Core Syntax and Examples
🧩 Definition
Exit code 126 = “Command found but not executable.”
Example 1 – No Execute Permission on Script
touch test.sh
echo "echo Hello World" > test.sh
./test.sh
echo $?
Expected Output:
bash: ./test.sh: Permission denied
126
Explanation: The script exists but lacks execute permission. Fix:
chmod +x test.sh
./test.sh
Output after fix:
Hello World
0
Use Case (WordPress VPS):
Backup script /root/backup.sh scheduled by cron fails with 126 because chmod +x wasn’t applied.
Example 2 – File Exists but Not a Valid Executable
echo "Not a binary" > fakebinary
chmod +x fakebinary
./fakebinary
echo $?
Expected Output:
bash: ./fakebinary: cannot execute binary file: Exec format error
126
Explanation:
File is marked executable but not in a valid binary or script format.
Bash can’t interpret it → returns exit code 126.
Use Case:
A corrupted downloaded script (e.g., truncated .sh file from GitHub) leads to 126 when run.
Example 3 – Wrong Interpreter in Shebang
echo '#!/wrong/path/bash' > wrongshebang.sh
echo 'echo OK' >> wrongshebang.sh
chmod +x wrongshebang.sh
./wrongshebang.sh
echo $?
Expected Output:
bash: ./wrongshebang.sh: /wrong/path/bash: bad interpreter: No such file or directory
126
Explanation: Script header points to a non-existent interpreter. Even though permissions are correct, Bash cannot execute → code 126. Use Case: Misconfigured shebang after moving environment paths in VPS migration.
Example 4 – Restricted Directory Permissions
mkdir /root/testdir
chmod 000 /root/testdir
cd /root/testdir
echo $?
Expected Output:
bash: cd: /root/testdir: Permission denied
126
Explanation: You have no permission to access the directory, triggering “cannot execute” context.
Example 5 – Executing a Directory
mkdir myfolder
chmod +x myfolder
./myfolder
echo $?
Expected Output:
bash: ./myfolder: Is a directory
126
Explanation:
Directories can’t be executed, even with +x.
The OS treats it as “found, but not executable.”
Use Case (WordPress VPS):
Trying to run /var/www/html/wp-content/ accidentally as a command path.
Example 6 – SELinux or AppArmor Restriction (Advanced)
./myscript.sh
echo $?
Expected Output (on restricted systems):
bash: ./myscript.sh: Permission denied
126
Explanation: SELinux/AppArmor policies can block execution even if permissions look fine. Fix: Check with:
sestatus
aa-status
4. Common Scenarios That Trigger Exit Code 126
| Scenario | Example | Description |
| Missing execute permission | ./script.sh | File exists but not executable |
| Invalid binary format | ./file.txt | Non-binary marked as executable |
| Wrong shebang | #!/usr/bin/bashwrong | Bad interpreter path |
| Directory executed | ./foldername | Directory not executable |
| SELinux/AppArmor block | ./myscript.sh | Security restriction |
| Mounted filesystem (noexec) | /mnt/nfs/script.sh | Filesystem mount denies execution |
5. WordPress VPS Use Cases
| Use Case | Example | Explanation |
| Cron job fails | bash: /root/wpbackup.sh: Permission denied | Script not executable |
| Plugin deploy automation | /scripts/deploy.sh returns 126 | Missing chmod +x |
| Maintenance task | wpcli.sh returns 126 | Wrong shebang path after migration |
| Service restart | /usr/local/bin/olsrestart returns 126 | Permission or ownership issue |
| Mounted volume | /mnt/scripts/wp-sync.sh | Mounted drive flagged noexec |
6. Troubleshooting Matrix
| Symptom | Likely Cause | Fix |
| “Permission denied” | No execute permission | Run chmod +x filename |
| “Exec format error” | File not valid script | Check file filename |
| “Bad interpreter” | Invalid shebang | Verify path with which bash |
| “Is a directory” | Executing folder | Run a file, not directory |
| “Operation not permitted” | SELinux or mount restriction | Check with mount or sestatus |
| Works manually, fails via cron | Different environment/path | Use full path (e.g., /usr/bin/bash) |
7. Best Practices
- Always verify permissions before running:
ls -l script.sh
Should show:
rwxr-xr-x
- Set execute permission safely:
chmod 755 script.sh
- Use valid shebang:
#!/bin/bash
- Validate file type before execution:
file script.sh
- Avoid running from
noexecmount:
mount | grep noexec
- Log failures for cron-based scripts:
./script.sh || echo "$(date): script failed with exit 126" >> /var/log/script_error.log
8. Quick Lab – Permission Scenario
Step 1: Create a script without permission
echo "echo 'Backup Started'" > /root/backup.sh
Step 2: Run it directly
/root/backup.sh
echo $?
Expected Output:
bash: /root/backup.sh: Permission denied
126
Step 3: Fix it
chmod +x /root/backup.sh
/root/backup.sh
echo $?
Expected Output:
Backup Started
0
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 error |
126 | Command cannot execute | Permission or binary issue |
127 | Command not found | Missing or wrong path |
130 | Terminated manually | User stopped (Ctrl+C) |
10. Mini-Quiz
- What does exit code
126mean in Bash? - How does
126differ from127? - Which command fixes “Permission denied” for a valid script?
- What happens if your script’s shebang points to
/usr/bashwrong? - How can you check whether a filesystem is mounted as
noexec?
Would you like me to continue next with Exit Status Code = 127 (Command Not Found) — another critical one for diagnosing WP-CLI, cron jobs, and PATH issues?