Andy Andy - 4 months ago 16
Linux Question

How can I configure the Linux terminal to only line feed (and not carriage return) when outputting a "\n"?

When I run a program that outputs "\n" to the terminal, I'd like to configure the terminal beforehand (perhaps via stty) to not do a Carriage Return, but to only move the cursor down a row. To actually consider it a Line Feed, and not to perform a Carriage Return.

For example, if the program prints "123\n456", I would like to see:

123
456


but I, of course, currently see:

123
456

Answer

man stty says:

   * [-]onlcr
          translate newline to carriage return-newline

So we can turn it off, print something and turn it on again:

$ stty -onlcr; printf '\rfoo\nbar\r\n'; stty onlcr
foo
   bar