bash 期望中的 \n 和 \r 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26187170/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:28:17  来源:igfitidea点击:

Difference between \n and \r in expect?

linuxbashexpect

提问by Jasmine Lognnes

In this particular script they use \n.

在这个特定的脚本中,他们使用\n.

#!/usr/bin/expect

set password [lindex $argv 0]

spawn asadmin enable-secure-admin
expect "admin"
send "admin\n"
expect "password"
send "$password\n"
expect eof
exit

Question

Could \rjust as well have been used? If not, what are the differences?

也能用\r吗?如果不是,有什么区别?

回答by Kenster

\nis linefeed, Ctrl-J or character 012. \ris carriage return, Ctrl-M or character 015.

\n是换行、Ctrl-J 或字符 012。\r是回车、Ctrl-M 或字符 015。

In an interactive unix context, when you're typing them (or simulating typing them, as with expect), they are interchangeable. Linefeed is the formal line-terminator character, but tty devices normally translate carriage return to linefeed on input. On your keyboard, the BKWA (big key with arrow which might be labelled "enter" or "return"), sends a Ctrl-M, which the tty device will translate to Ctrl-J. If your BKWA is broken, you can actually type Ctrl-M or Ctrl-J and it'll work just as well.

在交互式 unix 上下文中,当您键入它们(或模拟键入它们,如预期)时,它们是可以互换的。换行符是正式的换行符,但 tty 设备通常在输入时将回车符转换为换行符。在您的键盘上,BKWA(带箭头的大键,可能标记为“输入”或“返回”)发送一个 Ctrl-M,tty 设备会将其转换为 Ctrl-J。如果您的 BKWA 坏了,您实际上可以键入 Ctrl-M 或 Ctrl-J,它也能正常工作。

On output they're not interchangeable. As I said, linefeed is the formal line terminator character, so programs (and text files) will indicate end-of-line with a linefeed. When a linefeed is output to a tty, the tty will normally translate it into a carriage-return-linefeed pair.

在输出上,它们不可互换。正如我所说,换行符是正式的行终止符,因此程序(和文本文件)将用换行符指示行尾。当换行符输出到 tty 时,tty 通常会将其转换为回车换行符对。

When the characters actually reach the display device, carriage return moves the cursor to the beginning of the row, while linefeed moves it down a row.

当字符真正到达显示设备时,回车将光标移动到行的开头,而换行将其向下移动一行。