Meaning of \r on linux systems

Baz picture Baz · Feb 20, 2013 · Viewed 25.8k times · Source

I'm looking at some linux specific code which is outputting the likes of:

\r\x1b[J>

to the std io.

I understand that <ESC>[J represents deleting the contents of the screen from the current line down, but what does \r do here?

I'm also seeing the following:

>user_input\n\r>

where user_input is the text entered by the user. But what is the purpose of the \r here?

Answer

Some programmer dude picture Some programmer dude · Feb 20, 2013

The character '\r' is carriage return. It returns the cursor to the start of the line.

It is often used in Internet protocols conjunction with newline ('\n') to mark the end of a line (most standards specifies it as "\r\n", but some allows the wrong way around). On Windows the carriage-return newline pair is also used as end-of-line. On the old Macintosh operating system (before OSX) a single carriage-return was used instead of newline as end-of-line, while UNIX and UNIX-like systems (like Linux and OSX) uses a single newline.