The exit code 126 in Cygwin indicates that the system found the executable but lacks permission to run it. This is a security feature where Windows prevents scripts or binaries from executing due to incorrect file attributes or ownership issues. You can resolve this by correcting file permissions, ensuring proper line endings, and verifying that the files are not blocked by Windows Defender.
Understanding Exit Code 126
When you attempt to execute a script or binary in Cygwin and receive/bin/bash: Exit 126. Failed to Run /bin/bash: Permission Denied, it is crucial to distinguish between two similar error codes. Exit code 127 means "command not found," implying the file does not exist or is not in your path. Exit code 126 means "permission denied," meaning the file exists, but the operating system refuses to execute it.
In the context of Windows 10 and Cygwin, this refusal usually stems from one of three causes:
1. The file lacks the executable bit set in the Cygwin environment.
2. The file has Windows-specific security attributes (like "Mark of the Web") that prevent execution.
3. The file contains invalid characters, such as Windows-style carriage returns (CRLF) instead of Unix-style line feeds (LF), which confuses the bash parser.
This issue often arises after copying files from another machine, downloading archives, or using version control systems that do not preserve Unix permissions correctly. It is a common friction point for users migrating from Linux environments to Windows. For deeper insights into how Cygwin handles these specific security quirks, you may want to review Troubleshooting Cygwin's Weird Behavior on Security Permissions.
Step-by-Step Troubleshooting Guide
Follow these steps in order to isolate and fix the permission denied error. Start with the simplest fixes before moving to more complex attribute corrections.1. Verify File Existence and Path
First, ensure the file actually exists at the specified location. Use thels -l command to check if the file is visible and see its current permissions.
ls -l /path/to/your/script.sh
If the file is listed, note the permission string. It should start with -rwxr-xr-x or similar, indicating read, write, and execute rights. If it starts with --w--w--w- or lacks the x (execute) bits, this is likely the primary cause.
2. Add Execute Permissions
Cygwin relies on POSIX permissions. If you created the file using Notepad or copied it from Windows Explorer, it may not have the execute bit set. Use thechmod command to add execute permissions for the user, group, and others.
chmod +x /path/to/your/script.sh
After running this command, try executing the script again. If it works, the issue was simply missing execute flags. If you still get an error, proceed to the next step.
3. Check for Windows Line Endings (CRLF)
Windows uses Carriage Return + Line Feed (CRLF) for text files, while Unix/Linux uses only Line Feed (LF). If your script was edited on Windows, it likely contains CRLF sequences. Bash interprets the carriage return character (\r) as part of the command name. For example, instead of running ./script.sh, it tries to run ./script.sh\r, which fails with a permission or not found error.
To check for this, use the file command:
file /path/to/your/script.sh
If the output says "with CRLF line terminators," you need to convert the file. You can use dos2unix if installed, or configure your editor to save with Unix line endings.
dos2unix /path/to/your/script.sh
Alternatively, you can use sed to remove carriage returns:
sed -i 's/\r$//' /path/to/your/script.sh
4. Remove Windows "Mark of the Web"
Windows adds a zone identifier to downloaded files or files moved from other drives to prevent automatic execution. This is known as the "Mark of the Web." Cygwin respects these NTFS alternate data streams, which can block execution even if POSIX permissions are correct. You can remove this mark using PowerShell. Open PowerShell as Administrator and run:Unblock-File -Path "C:\cygwin64\home\user\script.sh"
Replace the path with the actual path to your script. This removes the zone identifier that Windows uses to flag the file as potentially unsafe.
5. Check Antivirus and Windows Defender
Sometimes, real-time protection software interferes with Cygwin's execution engine. If the above steps fail, temporarily disable your antivirus or Windows Defender real-time protection to see if the script runs. If it does, add an exclusion for your Cygwin home directory or the specific script. For more information on managing permissions in Cygwin, consider reading Bash Binutils Configure Permission Denied, which covers similar permission hurdles during compilation.Preventing Future Issues
To avoid encountering this error repeatedly, adopt the following practices:- Use Proper Editors: Use editors like VS Code, Sublime Text, or Vim that allow you to explicitly set line endings to LF. Avoid saving scripts in standard Notepad unless you are sure to change the encoding settings.
- Version Control: Use Git with core.autocrlf set appropriately. In a Cygwin environment, setting
core.autocrlf=inputensures that files checked out retain their Unix line endings regardless of the platform they were committed from. - Consistent Permissions: When cloning repositories, ensure that the repository preserves executable bits. Some platforms strip these bits when syncing with Windows filesystems.
Frequently Asked Questions
Why does chmod not fix the error?
chmod only modifies POSIX permissions stored in Cygwin's internal database. If the underlying Windows NTFS attributes (like the Mark of the Web) or line ending issues are present, chmod will not override them. You must address the Windows-specific markers separately using tools like Unblock-File or dos2unix.
Can I ignore the Mark of the Web permanently?
You can configure Windows to automatically unblock files upon download via Group Policy, but this is generally discouraged for security reasons. A safer approach is to use PowerShell scripts to unblock specific directories used for development, rather than disabling the feature globally.Does this error occur in WSL as well?
While less common, WSL can also face permission issues if files are shared directly between Windows and Linux partitions without proper handling. However, WSL2 typically handles line endings and permissions differently than Cygwin. For related troubleshooting in WSL, see Bash Source Command Silently Fails in WSL2 vs Direct Executions in Shell?.How do I check if my script has CRLF endings?
Run thefile command on the script. If it outputs "with CRLF line terminators," it has Windows-style endings. You can also open the file in a hex editor to look for the byte sequence 0D 0A (CR+LF) instead of just 0A (LF).
Is it safe to disable Windows Defender for Cygwin?
It is not recommended to disable Windows Defender entirely. Instead, add an exclusion for your Cygwin root directory (e.g.,C:\cygwin64) or your project folders. This allows Cygwin to execute scripts freely while keeping the rest of your system protected against malware.