[Top][All Lists]

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

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

From: Stephane Chazelas
Subject: Re: [Help-bash] Using single quotes to escape a newline
Date: Thu, 30 Jul 2015 08:29:56 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2015-07-29 17:28:56 -0700, Michael Convey:
> Are you saying that when echo sends its output to stdout, and only when
> stdout is the terminal, the kernel converts the <newline> to \r\n? In \r\n
> does \r = <CR> (carriage return) and \n = <newline>​?
>  In this case, is \n interpreted as <LF> (line feed)?

echo always sends its output to stdout, to the file descriptor
1, it doesn't have any option to send it to any other file
descriptor (ksh/zsh's "print" buitin have a -u option for that).

stdout is the file descriptor 1, it has nothing to do with a
terminal, other than terminal emulators do start a shell with
its stdout connected to the terminal device.

Even when you do:

echo foo >&2

echo still writes to its stdout, it's just that we've redirected
stdout to the same resource as open on stderr for the duration
of echo.

Now when that fd 1 is open to a terminal device (like
/dev/pts/4) which has a "terminal line discipline" pushed onto
it (which is the default for pseudo-tty or serial devices, but
could also be "mouse" or "SLIP" line discipline (see
ldattach(1))), and that line discipline is configured with
"opost" and "onlcr" enabled (see the output of "stty -a"), then
if something like echo does:

   write(1, "\n");

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


$ printf 'foo\nbar\n'


$ stty -onlcr; printf 'foo\nbar\n'; stty sane


reply via email to

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