Meaning of \r on linux systems

bash, c++, linux, terminal, vt100

Solution

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

It is often used in Internet protocols in 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.

Problem

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?

Original source