Newline

Also known as: line ending · CRLF

On this page 7

A newline is the control character or character sequence that ends a line of text and tells software where the next line begins. Also called a line ending or end-of-line marker, it is encoded as LF on Unix-like systems, as CRLF on Windows and in many internet protocols, and historically as CR on classic Mac OS. In software interoperability, the newline is one of the most common sources of silent incompatibility, because two files can look identical on screen while differing byte by byte.

Line endings matter wherever text crosses a boundary between operating systems, editors, version control systems, network protocols and parsers. A mismatch rarely corrupts data outright, but it breaks scripts, inflates diffs, invalidates checksums and confuses parsers that expect one convention.

How newline characters are encoded

A newline is encoded with one or two ASCII control codes that predate graphical displays. Line feed (LF, 0x0A) originally advanced the paper of a teleprinter by one line, while carriage return (CR, 0x0D) moved the print head back to the left margin. Mechanical terminals needed both actions, which is why early standards paired them as CR followed by LF.

Later operating systems simplified the convention in different ways. Unix kept only LF and let the terminal driver translate it to CRLF on output, classic Mac OS kept only CR, and MS-DOS inherited the full CRLF pair from CP/M. Unicode added further line separators, so a fully conformant text processor must recognize several distinct sequences.

The following table lists the line-ending sequences a robust parser may encounter.

SequenceBytes in ASCII or UTF-8Typical origin
LF0ALinux, macOS, BSD, most source code repositories
CRLF0D 0AWindows text files, HTTP, SMTP, CSV per RFC 4180
CR0DClassic Mac OS up to version 9, some legacy devices
NELC2 85 in UTF-8EBCDIC mainframe data converted to Unicode
LS and PSE2 80 A8 and E2 80 A9Unicode line and paragraph separators
Common newline sequences and where they appear

Newline conventions across platforms and protocols

Each platform treats its own newline convention as the default, and most network protocols mandate CRLF regardless of the host system. The POSIX standard defines a line as a sequence of characters terminated by LF, so a Unix text file should end with a final newline. Tools such as wc -l count LF bytes, which means a file without a trailing newline reports one line fewer than it visibly contains.

Protocol specifications are explicit about line endings because the receiving side must parse headers reliably. HTTP/1.1 header lines, SMTP commands, FTP control messages and MIME multipart boundaries all use CRLF. Many implementations accept a bare LF for robustness, but lenient parsing has caused security flaws such as request smuggling and header injection.

The table below summarizes the expected convention in common environments.

EnvironmentExpected newlineTolerance of other forms
Linux and macOS shellsLFCR becomes part of the data
Windows Notepad (current)CRLF by defaultDisplays LF and CR files correctly
HTTP/1.1 headersCRLFBare LF often accepted, sometimes rejected
SMTP and email bodiesCRLFBare LF rejected by strict servers
CSV per RFC 4180CRLFMost libraries accept LF
JSON and YAMLAnyNewlines treated as whitespace
Expected line endings by environment

How software translates line endings

Translation happens at several layers, and each layer can silently rewrite bytes. The C standard library distinguishes text mode and binary mode: on Windows, text mode converts LF to CRLF on write and CRLF to LF on read, while binary mode leaves bytes untouched. Python, Java and .NET offer similar universal newline handling when reading text.

Version control systems add another layer. Git can normalize line endings through the core.autocrlf setting or, more reliably, through a .gitattributes file with text=auto and per-pattern eol rules. Repository-level attributes are preferred because they travel with the code instead of depending on each developer’s local configuration.

  • FTP ASCII mode converts line endings during transfer, while binary mode preserves them.
  • Editors usually detect the existing convention and preserve it when saving.
  • Converters such as dos2unix and unix2dos rewrite files explicitly in batch.
  • Archivers may convert text members when a text flag is set in the archive.

Interoperability problems caused by newline mismatches

Newline mismatches produce recognizable symptoms once a developer knows what to look for. A shell script saved with CRLF fails on Linux with a confusing message such as /bin/bash^M: bad interpreter, because the carriage return becomes part of the interpreter path. A diff tool may show every line of a file as changed after a single editor save.

Because the cause is invisible in most editors, the following table pairs common symptoms with their underlying cause and fix.

SymptomLikely causeRemedy
Script fails with ^M in errorCRLF in a Unix scriptConvert to LF, enforce via .gitattributes
Whole-file diff after small editEditor switched line endingsNormalize repository, configure editor
Checksum or signature mismatchTransfer tool converted endingsTransfer in binary mode
Extra blank lines in outputCRLF read as two line breaksUse universal newline reading
String comparison failsTrailing CR left in parsed valuesStrip CR when splitting lines
Email rejected by serverBare LF in SMTP dataEmit CRLF in protocol output
Symptoms of newline mismatches and their remedies

Newline issues often appear together with character encoding problems such as a byte order mark or mojibake, since both stem from undocumented assumptions about how text is stored. Diagnosing them together with a hex viewer saves time.

Best practices for consistent line endings

The most reliable approach is to choose one convention per artifact type and enforce it automatically. For source code, LF is the de facto standard across modern toolchains, including on Windows, where current compilers, editors and Git clients handle LF without difficulty. Windows batch files and some legacy tools still require CRLF, so exceptions should be declared explicitly.

  • Declare rules in .gitattributes, for example * text=auto eol=lf with *.bat eol=crlf.
  • Add an EditorConfig file with end_of_line and insert_final_newline settings.
  • Validate in continuous integration so mixed line endings fail the build early.
  • Read leniently, write strictly: accept LF, CRLF and CR on input, emit one documented form on output.
  • Document the convention in any file format or API specification that exchanges text.

Never normalize binary data by accident. Marking images, archives and compiled files as binary in version control and transfer tools prevents irreversible corruption caused by line-ending conversion.

In simple terms

A newline is an invisible marker that says the line ends here. When you press Enter, the computer stores this marker inside the text. The catch is that different systems write it differently: Windows uses two hidden characters, while Linux and macOS use one.

Think of it like countries that drive on different sides of the road. Each system works fine on its own, but problems start at the border. The text looks the same on screen, yet the hidden bytes differ, so programs that expect one style can stumble over the other.

A practical example

A development team writes deployment scripts on Windows laptops and runs them on Linux build servers. After a new developer joins with Git configured to convert line endings to CRLF on checkout, a commit reintroduces CRLF into a shell script. The next deployment fails with a bad interpreter error, and the pipeline stops for an hour while the team searches for a problem they cannot see in their editors.

The team inspects the file with a hex viewer, finds the 0D 0A byte pairs, and converts the script to LF. To prevent a repeat, they add a .gitattributes file with * text=auto eol=lf and an exception for .bat files, plus an EditorConfig file and a CI check that rejects CRLF in shell scripts. The failures stop, and diffs shrink to the lines people actually changed.