Open In App

Difference Between Carriage Return (CR) and Line Feed (LF) ? How it is used in Different Operating System?

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

While using text files in scripts on different operating systems, you may be concerned with different newline characters, which can also lead to confusion and file incompatibility. Two primary characters are used to denote the end of a line: Carriage Return (CR) and another command called Line Feed (LF).

This article will explain their History, Usage in Different Operating Systems, Related Issues, what they are, when they were introduced, and how one can use them to address various issues arising from the e of CR, and LF.

What is Carriage Return (CR)?

The Carriage Return (CR) is a control character found in the American Standard Code for Information Interchange, mainly represented by the ASCII code set. The precious method’s purpose is to navigate the cursor to the initial position of the line. In the past, this action resembled the operation of typewriters, in which the carriage is that part of the instrument that holds the paper and moves back to the left of the paper.

The carriage Return character is used inside the text files and line-oriented operations to move the cursor down one line but does not reset the horizontal position of the cursor. This makes text modifications easier, especially in the terminal options or any application that requires a new line to be rewritten without going to the next line.

  • Symbol: \r
  • ASCII Code: 13

Functions of CR

  • Typewriter Analogy: Brings the paper down one line.
  • Computing Use: Moves the cursor down to the next line without returning to the beginning.
  • Example Usage: "\n" is used to create new lines in text files or outputs, such as separating paragraphs or list items.

What is Line Feed (LF)?

Another control character in the communicated ASCII code set is Line Feed (LF). Its main use is to advance the cursor to a new line while not getting back to the start of the line. In terms of typewriter similarity, this character propels the paper down a line.

In computing, the Line Feed character is used for line oriented actions and text manipulation. It enables the system to drop the cursor to the next line but does not reset the horizontal position of the cursor. This makes it a very important character in differing lines of text in files particularly in Unix related systems since serves as the standard line terminator.

  • Symbol: \n
  • ASCII Code: 10

Functions of LF

  • Typewriter Analogy: Advances the paper down by one line.
  • Computing Use: Takes the cursor down to the next line without returning to the beginning.
  • Example Usage: The “\n” creates a new line in form of a text file or the output such as; paragraphs or the list items.

Historical Background of CR and LF

Based on the analysis of the obtained results, let us remember that both CR and LF originated from the typewriter and early computing days.

  • Carriage Return (CR, \r, ASCII 13): A Carriage return, takes the paper carriage back to the beginning of the line. In computing this control character repositions the cursor to the beginning of the line without going down to the next line.
  • Line Feed (LF, \n, ASCII 10): In a typewriter, the line feed will help advance the paper by one line. In computing, this tip moves the cursor down to the next line without going back to the start of the line again.

These characters were used in the early computer systems specifically for the formatting of text files. However, it is seen that different systems employed these characters in various combinations.

Usage Across Operating Systems

1. Unix/Linux (LF)

  • In regards to the Unix and Linux operating systems, Line Feed or \n is used to signify the last line of a document. This allows for easier handling and processing of the text to get the wanted results.
  • Example:
Line one\n
Line two\n

2. Windows (CRLF)

  • For operating systems that are Windows based, they use the Carriage Return and Line Feed between them, symbolized by \r\n. This convention relates to the file extensions and is inherited from CP/M, and used in teletype systems and MS-DOS.
  • Example:
Line one\r\n
Line two\r\n

3. Classic Mac OS (CR)

  • Older versions of Mac OS prior to those which were named Mac OS X ended lines with the Carriage Return character, denoted as \r.
  • Example:
Line one\r
Line two\r

OS X and later versions of macOS adopted the Unix convention of using LF (\n).

Implications and Compatibility Issues

It is also important to note that text files that are transfer across different operating systems may run into newline character problems. For instance, a file that was developed using Windows will appear distorted when opened in Unix and the other way around.

  • Unix/Linux Systems: Specifically, when a Windows text file with CRLF is opened in a Unix/Linux system, the CR character will be represented as ^M or other symbols, which may influence people’s perception of the text and result in readability problems.
  • Windows Systems: On the other hand a Unix/Linux text files which contain only the linefeed (LF) presented as a new line may not be well viewed in some text editors that look for Carriage Return Line Feed (CRLF) at the end of lines in a windows text file.

Tools and Methods to Handle Newline Characters

1. Text Editors

All modern text editors: VS Code, Sublime Text, Notepad ++, etc. have no problems with different newline characters, and can even switch between them. Sometimes they have options, or parameters, usually introduced with the –‘/’ character, that allow to set the desired newline mode.

2. Command Line Tools

dos2unix: Converts text files with CRLF line endings to LF

dos2unix filename.txt

unix2dos: Converts text files with LF line endings to CRLF.

unix2dos filename.txt

tr: A command-line utility to translate or delete characters.

tr -d '\r' < windowsfile.txt > unixfile.txt

Practical Examples and Scenarios

1. Collaborative Projects

It is not surmounting for team members of a software development project to work on different operating systems. Newline characters may also be treated together by version control systems like Git whenever it is configured properly.

Git Configuration:

# Automatically convert CRLF to LF on commit and LF to CRLF on checkout
git config --global core.autocrlf true

# Convert CRLF to LF on commit but do not modify line endings on checkout
git config --global core.autocrlf input

# No conversion
git config --global core.autocrlf false

2. Web Development

When deploying web applications, it is necessary to pay attention to the newline characters, so as to avoid the emergence of various problems that may occur in the interpretation of the script on different servers.

3. Scripting and Automation

In a script for distinct OS, a role of files that have different newline characters may be created. Anticipating and adding the conversion related steps in the newline can lead to efficient execution.

Difference Between Carriage Return (CR) and Line Feed (LF)

Feature

Carriage Return (CR)

Line Feed (LF)

Symbol

\r

\n

ASCII Code

13

10

Historical Use

Moves the cursor to the start of the line

Moves the cursor down one line without moving to the start of the line

Typewriter Analogy

Return the carriage to the start of the line

Advance the paper by one line

Unix/Linux

Rarely used alone

Standard (\n)

Windows

Not used alone

Not used alone

Classic Mac OS (pre-OSX)

Standard (\r)

Not used

Modern macOS (OSX and later)

Rarely used alone

Standard (\n)

Common Compatibility Issue

Displays as ^M or similar on Unix/Linux systems when reading Windows files

May not display properly in some Windows text editors

Conversion Tools

tr -d '\r'

unix2dos for adding CR

Common Use Cases

Historically in older Mac systems and some protocols

Unix/Linux text files, modern macOS

Conclusion

It becomes very important for developing interfaces that deal with text files across the platforms to understand what really Carriage Return (CR) is and what exactly Line Feed (LF) is. Unix/Linux uses LF, Windows use CRLF and the old Mac operating system use CR. There are some concerns, each time files are transferred from one of these systems to the other; however, powerful text editors, command-line utilities, and libraries within programming languages can deal with newline characters properly. From these differences, one is thus able to work with text files in any environment without straying into the traps that can cause unmanageable problems.


Next Article

Similar Reads