Skip to main content

📘 Exit Status Code = 126 in Bash



🎯 What You Will Learn

  1. Understand the meaning and root cause of exit status code = 126 in Bash.
  2. Learn how it differs from other execution-related errors like 127 and 1.
  3. Identify situations when a command exists but cannot execute (permissions, binary issues).
  4. Debug permission and execution errors using CLI tools (ls -l, chmod, file).
  5. Apply solutions in WordPress VPS contexts, especially with cron jobs, scripts, and WP-CLI automation.
  6. Learn practical fixes and preventive strategies to avoid error 126 in production.

1. 5W + 1H Framework

ElementDescription
WhatExit status 126 means the command was found but cannot execute — often due to missing execute permissions, wrong binary format, or restricted shell policy.
WhyIt signals Bash located the command or script but OS denied execution (permissions, architecture, or security context).
WhoSystem administrators, Bash developers, and DevOps managing executable scripts.
WhenWhen running a script or binary file without +x permission, or using incorrect interpreter path.
WhereCommon in VPS cron jobs, automation scripts, plugin updates, or any .sh file execution.
HowCheck 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, and file.
  • Have a test VPS or Linux environment with a writable /root or /home directory.

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

ScenarioExampleDescription
Missing execute permission./script.shFile exists but not executable
Invalid binary format./file.txtNon-binary marked as executable
Wrong shebang#!/usr/bin/bashwrongBad interpreter path
Directory executed./foldernameDirectory not executable
SELinux/AppArmor block./myscript.shSecurity restriction
Mounted filesystem (noexec)/mnt/nfs/script.shFilesystem mount denies execution

5. WordPress VPS Use Cases

Use CaseExampleExplanation
Cron job failsbash: /root/wpbackup.sh: Permission deniedScript not executable
Plugin deploy automation/scripts/deploy.sh returns 126Missing chmod +x
Maintenance taskwpcli.sh returns 126Wrong shebang path after migration
Service restart/usr/local/bin/olsrestart returns 126Permission or ownership issue
Mounted volume/mnt/scripts/wp-sync.shMounted drive flagged noexec

6. Troubleshooting Matrix

SymptomLikely CauseFix
“Permission denied”No execute permissionRun chmod +x filename
“Exec format error”File not valid scriptCheck file filename
“Bad interpreter”Invalid shebangVerify path with which bash
“Is a directory”Executing folderRun a file, not directory
“Operation not permitted”SELinux or mount restrictionCheck with mount or sestatus
Works manually, fails via cronDifferent environment/pathUse full path (e.g., /usr/bin/bash)

7. Best Practices

  1. Always verify permissions before running:
ls -l script.sh

Should show:

  • rwxr-xr-x
  1. Set execute permission safely:
chmod 755 script.sh

  1. Use valid shebang:
#!/bin/bash

  1. Validate file type before execution:
file script.sh

  1. Avoid running from noexec mount:
mount | grep noexec

  1. 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 CodeMeaningDescription
0SuccessCommand executed successfully
1General errorNon-specific failure
2Misuse of shell builtinsSyntax or structure error
126Command cannot executePermission or binary issue
127Command not foundMissing or wrong path
130Terminated manuallyUser stopped (Ctrl+C)

10. Mini-Quiz

  1. What does exit code 126 mean in Bash?
  2. How does 126 differ from 127?
  3. Which command fixes “Permission denied” for a valid script?
  4. What happens if your script’s shebang points to /usr/bashwrong?
  5. 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?