newlinecontrol-characters

Why doesn't \r\n / CRLF result in the line being reread in perpetuity?


If CR (\r) moves the cursor to the front of the line, and LF (\n) moves the cursor to the next line, why is LF consumed, rather than the line being re-read?

e.g.: hello world\r\n

To me, this functionality implies that the cursor is not necessarily related to the consumption of the file/text/buffer, but how this all cooperates is not immediately apparent to me, either.

What gives?


Solution

  • To answer my own question (and to provide some context, since the question is silly...):

    The original intent of the question was to understand why/how we can iterate over lines in files without ever getting into an infinite loop, as - naively - it might be safe to assume that seeing \r or \n would result in an immediate action by whatever entity (program, console, etc.) is viewing the text. This is silly (and incorrect!).

    Given Hello World!\r\n - as far as any program (or otherwise) is concerned, this is just a string of bytes. Even the \r, \n chars are just that - additional characters. Why/how these characters (or bytes) have any impact on the general state of the terminal (as was my case of interest) is simply due to how the tty handles the interpretation of these chars at runtime. These chars have special behavior because they are control characters as specified by the environments that use them.