[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Using single quotes to escape a newline

From: Michael Convey
Subject: [Help-bash] Using single quotes to escape a newline
Date: Thu, 30 Jul 2015 08:04:24 -0700

On Thu, Jul 30, 2015 at 5:01 AM, Greg Wooledge <address@hidden>wrote:

> The behavior of a terminal (driver) is controlled by various flags that
> a shell program can read or set using stty(1).  stty calls this particular
> flag "onlcr" (Output NewLine => CarriageReturn).
> In the HP-UX manual:
>       onlcr (-onlcr)           Map (do not map) newline character to a
>                                carriage-return/newline character sequence
> on
>                                output.
> In the Debian manual:
>        * [-]onlcr
>               translate newline to carriage return-newline
> > In \r\n
> > does \r = <CR> (carriage return) and \n = <newline>????
> Yes.

​Very helpful, thank you. ​

On Thu, Jul 30, 2015 at 12:29 AM, Stephane Chazelas <
address@hidden> wrote:

> Which is writing the newline character to that device file. Then
> the tty line discipline (in the kernel) converts that to "\r\n"
> before sending it on the wire (in the case of a serial device)
> or making it available for reading on the master side of the
> pseudo-terminal for the terminal emulator to read in the case of
> xterm.
> Compare:
> $ printf 'foo\nbar\n'
> foo
> bar
> $ stty -onlcr; printf 'foo\nbar\n'; stty sane
> foo
>    bar

So in the first case, there's a <LF> and a <CR>; in the 2nd case, there's a
<LF> but no carriage return.

On Thu, Jul 2, 2015 at 2:03 AM, Stephane Chazelas <
address@hidden> wrote:

> When stdout is a terminal device ... will typically
> convert it to \r\n before it's  sent over the wire to the
> terminal (or ... terminal emulator like xterm). That's controlled by the
> onlcr stty setting. ... For terminals, the CR and LF characters are control
> characters: CR
> ​​
> moves the cursor to the first column and LF moves the cursor down.
> The LF character is the text line delimiter on Unix, so for
> lines to be displayed properly on a terminal, that translation
> has to occur.

In your first example:

$ printf 'foo\nbar\n'

The result is both a <CR> and a <LF>. If I understand you correctly, <CR>
moves the cursor to the beginning of the line. Is that what you meant by
"moves the cursor to the first column"? And <LF> moves the cursor down one
line. In the above example, is this what happens?
\n is converted to \r\n, and
\r = <CR>
\n = <LF> ??
How does the Line Feed occur? ​

In your second example, is it the '-onlcr' "translates newline to carriage
return-newline". However, there was no <CR> as evidenced by 'bar' not
starting at the beginning of the line, right?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]