The character representation of 'Newline' is different between different operating systems. Besides being a major source of software portability problems, a difference in newline representation can result in text files from another computer not being directly readable.

History of the newline

The old Westrex teletypes have a friction fed roll of paper, and a print head (like the golf ball in an electric type writer). There are two mechanical functions connected with a new line, one is to return the print head to the beginning of the line, and the other is to advance the paper roll by a line.

It makes sense to assign different codes to these, essentially distinct, functions. The ASCII code uses \015 for "Carriage Return" and \012 for "Line Feed". In order for the teletype to do the right thing, it must receive both codes.

If you switch one of these teletypes to local mode, it behaves like a type writer. Any keys you type are echoed. In this mode, you have to do both a carriage return and a line feed, for your new line.

When it comes to VDUs and command windows, the same rules apply. A carriage return moves the cursor to the left hand column of the screen (window), and line feed moves it down a line, scrolling if necessary.

The Fortran programming language reserves the first 'column' (byte) of every line, to provide what is called carriage control. Normally, this is a space, which causes the line to be printed between newlines (actually, on VMS Fortran and probably others, it causes a line feed to be sent to the printer before the line, and a carriage return afterwards). By varying the carriage control byte, you can alter this behaviour.

Representation in files

Most early operating systems stored text files as is, and files are copied to the printer byte for byte. The operating system that went away from this rule was Unix. Here, a single character, Line feed, was use to represent a new line. This means that translation needs to be done in order to print a file; the operating system adds extra carriage returns needed for display or printing. Similarly, the Macintosh used a carriage return.

VMS uses a different concept, as it considers lines in a text file as records, and it outputs a carriage return and line feed when it sees an end of record.

FTP and file sharing

Sharing files between computers running different operating systems highlights the problem with line termination. If you try and view a Unix text file with notepad, you will see a continuous splurge all on one line (wordpad and textpad are cleverer). Editing an MsDOS file with vi shows <^M> at the end of each line, which are the carriage returns.

To avoid these difficulties, ftp makes a distinction between ASCII (text) files and binary files. Any text files are converted to a common repesentation (the Unix form with line feed), as they are transmitted. Similarly, the line termination may need converting again as it arrives, into the destination platform's representation.

NFS has similar issues. Here, the standard is a network served Unix file system, hence any text files on it are expected to be linefeed line terminated. Samba however is designed to look like a Windows file share, hence will add carriage returns as the files are mapped from Unix.