bash \r 在 Linux 系统上的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14975661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Meaning of \r on linux systems
提问by Baz
I'm looking at some linux specific code which is outputting the likes of:
我正在查看一些特定于 linux 的代码,这些代码输出以下内容:
\r\x1b[J>
to the std io.
到 std io。
I understand that <ESC>[Jrepresents deleting the contents of the screen from the current line down, but what does \r do here?
我知道这<ESC>[J代表从当前行向下删除屏幕内容,但是 \r 在这里做什么?
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?
其中 user_input 是用户输入的文本。但是这里 \r 的目的是什么?
回答by Some programmer dude
The character '\r'is carriage return. It returns the cursor to the start of the line.
字符'\r'是回车。它将光标返回到行的开头。
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.
它通常在 Internet 协议中与换行符 ( '\n')结合使用以标记行的结尾(大多数标准将其指定为"\r\n",但有些允许错误的方式)。在 Windows 上,回车换行符对也用作行尾。在旧的 Macintosh 操作系统(OSX 之前)上,使用单个回车代替换行符作为行尾,而 UNIX 和类 UNIX 系统(如 Linux 和 OSX)使用单个换行符。
回答by Mikhail Vladimirov
Control character \rmoves caret (a.k.a text cursor) to the leftmost position within current line.
控制字符\r将插入符号(又名文本光标)移动到当前行内的最左侧位置。
回答by Jan Vorcak
From Wikipedia
来自维基百科
Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, '\r\n', 0x0D0A). These characters are based on printer commands: The line feed indicated that one line of paper should feed out of the printer thus instructed the printer to advance the paper one line, and a carriage return indicated that the printer carriage should return to the beginning of the current line. Some rare systems, such as QNX before version 4, used the ASCII RS (record separator, 0x1E, 30 in decimal) character as the newline character.
基于 ASCII 或兼容字符集的系统分别使用 LF(换行、'\n'、0x0A、十进制 10)或 CR(回车、'\r'、0x0D、十进制 13),或 CR 后跟LF(CR+LF,'\r\n',0x0D0A)。这些字符基于打印机命令: 换行表示应从打印机中送出一行纸,从而指示打印机将纸向前推进一行,回车表示打印机应返回到打印行的开头当前行。一些罕见的系统,例如版本 4 之前的 QNX,使用 ASCII RS(记录分隔符,0x1E,十进制 30)字符作为换行符。
回答by jim mcnamara
FWIW - this is a part of carriage control - from mainframe control words to Windows/UNIX/FORTRAN carriage control. Carriage control can be implemented at a language level like FORTRAN does, or system-wide like UNIX and Windows do.
FWIW - 这是马车控制的一部分 - 从大型机控制字到 Windows/UNIX/FORTRAN 马车控制。可以像 FORTRAN 那样在语言级别上实现回车控制,也可以像 UNIX 和 Windows 那样在系统范围内实现。
\narose from limitations of early PDP user "interfaces" - the tty terminal. Go to a museum if you want see one.
\n源于早期 PDP 用户“接口”——tty 终端的局限性。想看的话就去博物馆吧。
A very simple point: The difference between \n \r is explained above. But all of these explanations are really saying that carriage control is implementation dependent.
很简单的一点: \n \r 的区别上面已经解释过了。但是所有这些解释实际上都是在说运输控制是依赖于实现的。
The [Jis part of ANSI escape sequences and what they do on a "standards conforming tty terminal".
DOS used to have ANSI.SYS to provide: colors, underline, bold using those sequences.
这[J是 ANSI 转义序列的一部分,以及它们在“符合标准的 tty 终端”上的作用。DOS 曾经使用 ANSI.SYS 来提供:使用这些序列的颜色、下划线、粗体。
http://ascii-table.com/ansi-escape-sequences.php
Is a good reference for the question: what does some odd looking string in the output do?
是一个很好的参考问题: what does some odd looking string in the output do?
回答by Anders R. Bystrup
\ris carriage return. Similarly \nis linefeed.
\r是回车。同样\n是换行。

