Write tool creates files with CRLF line endings on WSL2/Linux, breaking shell script execution
Environment:
- Claude Code version: Current (as of 2025-01-02)
- OS: Linux (WSL2:
Linux 6.6.87.2-microsoft-standard-WSL2) - Platform:
linux(from environment context)
Description:
The Write tool creates files with Windows-style CRLF (\r\n) line endings even when running on Linux/WSL2 systems. This causes bash scripts to fail immediately after creation with errors like:
/bin/bash: line 1: ./script.sh: cannot execute: required file not found
Reproduction Steps:
- Use Write tool to create a bash script on WSL2/Linux:
<invoke name="Write">
<parameter name="file_path">/path/to/test.sh</parameter>
<parameter name="content">#!/bin/bash
echo "Hello World"
</parameter>
</invoke>
- Verify line endings:
file test.sh
# Shows: ASCII text, with CRLF line terminators
od -c test.sh | head -2
# Shows \r\n instead of \n
- Attempt to execute:
chmod +x test.sh
./test.sh
# Error: required file not found (due to \r in shebang)
Expected Behavior:
On Unix-like systems (Linux, macOS, WSL), the Write tool should:
- Create files with LF (
\n) line endings by default - Respect the platform's native line ending convention
- Alternatively, provide a parameter to specify line ending style
Current Workaround:
After every Write operation creating shell scripts:
dos2unix script.sh
# or: sed -i 's/\r$//' script.sh
Impact:
- Immediate execution failures: Scripts cannot be tested immediately after creation without manual post-processing
- Workflow friction: Requires extra steps after file creation
- Easy to forget: The error only appears when attempting to execute the script
- Partial mitigation via .gitattributes: Using
.gitattributesnormalizes line endings at commit time, but doesn't help the window between file creation and commit
Suggested Solution:
The Write tool should detect the platform and use appropriate line endings:
- Unix-like systems → LF (
\n) - Windows (non-WSL) → CRLF (
\r\n) - Optionally: Allow explicit line ending specification via parameter
Additional Context:
This issue affects any text file creation but is most critical for shell scripts where CRLF breaks the shebang (#!/bin/bash\r is not recognized). Similar issues may affect other interpreted scripts (Python, Ruby, etc.) though they're often more tolerant of CRLF.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗