Newline Tests
Free text files with LF (Unix), CRLF (Windows), CR (classic Mac) and mixed line endings, for testing conversion and cross-platform compatibility.
lf.txt / 704 B
crlf.txt / 714 B
cr.txt / 704 B
mixed.txt / 708 B
Why line-ending tests matter
Line endings differ across OSes: Unix/Linux/macOS use LF (n), Windows uses CRLF (rn), and Classic Mac OS used CR (r).
Use these to verify line-ending handling in version control (Git), text-processing tools, and programming languages.
📖 Where people get stuck
The same content written with LF (Unix), CRLF (Windows), CR (classic Mac) and a deliberate mix. Line endings change behaviour while staying invisible, which puts them among the harder text-processing bugs to trace. You cannot tell by looking, so trying the real thing is the short path.
| Case | What happens | What to do |
|---|---|---|
| git diff shows every line as changed | Change nothing at all and every line still differs if the endings went from LF to CRLF. An editor setting or the operating system does it silently. | Put * text=auto in .gitattributes and pin the files that care — *.sh text eol=lf for shell scripts and config. |
| A shell script with CRLF will not run | The trailing
is handed to the shell as part of the command. bash: $'
': command not found and syntax error near unexpected token are the usual symptoms. |
Run file script.sh; with CRLF line terminators confirms it. Strip them with tr -d '
' < in > out. Safer still, convert on the server rather than copying straight from Windows. |
| A stray survives at the end of each string | Reading a CRLF file line by line, many languages treat only
as the separator and leave the
in the value. Comparisons stop matching and numeric parsing fails. |
Trim as soon as you read. rtrim($line, "
") in PHP, line.rstrip() in Python. When the stray bytes are mid-file rather than at the ends, normalise with the line ending converter. |
Mixed files are the awkward case. LF through the first half and CRLF through the second is a perfectly ordinary result of two people editing on different machines. The file command reports only the first form it finds, so counting with grep -c $'
' is the reliable way to spot a mix.