How are \n and \r handled differently on Linux and Windows?
I think \n
moves the needle down, and \r
moves the needle to the beginning of a line (left align)? I'm not sure, though. So, if I'm wrong please correct me....
Anyway, I was told that Windows and Linux handle newlines
and carriage returns
differently. I would like to know how they handle them differently and some places where it's important to remember. Thanks for answering.
Solution 1:
I think \n moves the needle down, and \r moves the needle to the beginning of a line (left align)? I'm not sure, though
This is true, more or less, but mostly a historical curiosity. Originally, linefeed (LF) was used to advance the paper by one line on printers and hardcopy terminals (teleprinters); carriage return (CR) returned the print head to the start of the line.
This probably still works on modern printers when used in "text mode", but is otherwise of little relevance today.
Anyway, I was told that Windows and Linux handle newlines and carriage returns differently.
The difference is simply: OS designers had to choose how to represent the start of a new line in text in computer files. For various historical reasons, in the Unix/Linux world a single LF character was chosen as the newline marker; MS-DOS chose CR+LF, and Windows inherited this. Thus different platforms use different conventions.
In practice, this is becoming less and less of a problem. The newline marker is really only relevant for pograms that process "plain text", and there are not that many - it mostly only affects program source code, configuration files, and some simple text files with documentation. Nowadays most programs handling these kinds of files (editors, compilers etc.) can handle both newline conventions, so it does not matter which one you choose.
There are some cases where tools insist on "their" newline convention (e.g. Unix shell scripts must not use CR+LF), in which case you must use the right one.
Solution 2:
CR and LF
The American Standard Code for Information Interchange (ASCII) defined control-characters including CARRIAGE-RETURN (CR) and LINE-FEED (LF) that were (and still are) used to control the print-position on printers in a way analogous to the mechanical typewriters that preceded early computer printers.
Platform dependency
In Windows the traditional line-separator in text files is CR followed by LF
In old (pre OSX) Apple Macintosh systems the traditional line separator in text files was CR
In Unix and Linux, the traditional line-separator in text files is LF.
\n and \r
In many programming and scripting languages \n
means "new line". Sometimes (but not always) this means the ASCII LINE-FEED character (LF), which, as you say, moves the cursor (or print position) down one line. In a printer or typewriter, this would actually move the paper up one line.
Invariably \r
means the ASCII CARRIAGE-RETURN character (CR) whose name actually comes from mechanical typewriters where there was a carriage-return key that caused the roller ("carriage") that carried the paper to move to the right, powered by a spring, as far as it would go. Thus setting the current typing position to the left margin.
Programming
In some programming languages \n
can mean a platform-dependent sequence of characters that end or separate lines in a text file. For example in Perl, print "\n"
produces a different sequence of characters on Linux than on Windows.
In Java, best practise, if you want to use the native line endings for the runtime platform, is not to use \n
or \r
at all. You should use System.getProperty("line.separator")
. You should use \n
and \r
where you want LF and CR regardless of platform (e.g. as used in HTTP, FTP and other Internet communications protocols).
Unix stty
In a Unix shell, the stty
command can be used to cause the shell to translate between these various conventions. For example stty -onlcr
will cause the shell to subsequently translate all outgoing LFs to CR LF.
Linux and OSX follow Unix conventions
Text files
Text files are still enormously important and widely used. For example, HTML and XML are examples of text file. Most of the important Internet protocols, such as HTTP, follow text-file conventions and include specifications for line-endings.
Printers
Most printers other than the very cheapest, still respect CR and LF. In fact they are fundamental to the most widely used page description languages - PCL and Postscript.
Solution 3:
In short, was needed for printers, but now the OSes do it slightly differently. In most cases, it is fine to just do both CR and LF by doing \r\n
and in most cases, this will work fine.