Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between \n and \r in expect?

Tags:

linux

bash

expect

In this particular script they use \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 \r just as well have been used? If not, what are the differences?

like image 407
Jasmine Lognnes Avatar asked Oct 03 '14 21:10

Jasmine Lognnes


1 Answers

\n is linefeed, Ctrl-J or character 012. \r is carriage return, Ctrl-M or character 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.

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.

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.

like image 151
Kenster Avatar answered Oct 20 '22 09:10

Kenster