Line Endings

Line endings vary by operating system:

  • Unix/Linux/macOS: LF (Line Feed, \n, 0x0A)
  • Windows: CRLF (Carriage Return + Line Feed, \r\n, 0x0D 0x0A)
  • Classic Mac (pre-OS X): CR (\r, 0x0D)

Common Issues

Mixed line endings cause problems in version control diffs, shell scripts failing on Windows-originated files, and inconsistent behavior across team environments.

Git Configuration

# Convert to LF on commit, native on checkout (recommended for cross-platform)
git config --global core.autocrlf true   # Windows
git config --global core.autocrlf input  # Linux/macOS
 
# Or enforce via .gitattributes
* text=auto
*.sh text eol=lf
*.bat text eol=crlf

Quick Fixes

# Convert CRLF → LF
sed -i 's/\r$//' file.txt
dos2unix file.txt
 
# Convert LF → CRLF
unix2dos file.txt

Detection

file myfile.txt                    # Shows "with CRLF" if present
cat -A myfile.txt | head           # ^M at line ends = CR
od -c myfile.txt | grep -E '\\r'   # Raw byte inspection