My own answer, after learning from the answer and the comment by icarus:
You have to distinguish "a newline in a file" and "a newline in a console". In a console, the true newline is, counter-intuitively, CRLF, as we will see below.
In UNIX convetion, in text files LF means a newline, and vice versa, you mean a newline by LF. (By "you mean", I mean say in a natural language text.) In DOS CR+LF, and so on. Ok. Everyone knows it.
(Unix) console are more complicated. First you have to remember LF and CR are control codes, i.e. can be used to control a console, e.g. getting bold, color, etc.
If you feed a LF (\n, linefeed) to a console, then you get a newline. The catch is, well, the two catches are: (1) Consoles are double layered, so to say; they consist of a filter and a rendering part. (Ad hoc nomenclature.) The hidden (to ordinary users) filter translates LF to CRLF. (2) The renderer needs CRLF(\r\n) for a newline in ordinary sense. See below for more.
The typescript file created by the script (1)
command records the characters after the input to the console is filtered. That's why newlines in typescript is CRLF.
Details & misc. facts:
- The console renderer prints LF as "move down cursor one line" and CR as "move the cursor to the beginning of the line."
- You can turn off LF->CRLF conversion by
$ stty -opost
and efface it by $ stty opost
. "opost" is an abbreviation of "Output POSTprocessing".
- More precisely,
opost
does LF->LFCR when onlcr
is set. When onocr
is set, CR will be deleted when at the beginnig of line, etc. Ref: POSIX chap 11 "General Terminal Interface".
- To "Enter" key is bound to LF in Unix, called "Return" in keymap terminology. (See this question for the details.)
- There's also escape code variants;
man 4 console_codes
explains that "ESC D" (\eD) is linefeed, and "ESC E" (\eE) is newline. If you print them, "ESC D" is a "move cursor down", and "ESC E" a CR+LF, regardless of ±opost-ness.
To do some experiments, I recommend to write from a separate console. For example $ echo -ne '1st\n2nd\r\n3rd\n" > /dev/tty1
writes to the first non-X console, and /dev/pts/0
is the first X terminal. This is not the most convenient way, but least ambiguous.
/dev/tty*
as "tty's". It doesn't just stand for "teletype(writer)" any more. It evolved a new meaning. Language does that. – Feb 09 '17 at 15:29script
doesn't exist in a vacuum. – Feb 12 '17 at 06:58