11

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?

Jasmine Lognnes
  • 6,597
  • 9
  • 38
  • 58
  • 1
    Just to add to Kenster's great answer, Wikipedia has a good article on [newline](http://en.wikipedia.org/wiki/Newline) / EOL conventions, and the history behind them. Note that some Internet protocols prefer the `\r\n` combination for EOL, although most software will cope with plain `\n` and library routines for using such protocols may automatically translate the local EOL to the proper sequence. – PM 2Ring Oct 04 '14 at 07:28

1 Answers1

11

\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.

Kenster
  • 23,465
  • 21
  • 80
  • 106