On 01.11.2020 21:10, Chris F.A. Johnson wrote:
> On 2020-11-01, Janis Papanagnou wrote:
>> On 01.11.2020 19:07, Chris F.A. Johnson wrote:
>>> On 2020-10-31, Ed Morton wrote:
>>>> On 10/31/2020 5:19 PM, paris2venice wrote:
>>>> <snip>
>>>>> So what I discovered is that it is the '<<<' that is forcing the newline.
>>>>
>>>> Right, <<< has to produce a valid text line per the POSIX standard and
>>>> POSIX lines end in a newline. If it didn't do that then it'd be
>>>> undefined what any POSIX text processing tool did with the result.
>>>
>>> Is <<< now included in POSIX?
>>
>> I don't think so.
>>
>> (I interpreted Ed's comment as how lines are generally defined in POSIX.)
>>
>>> None of my attempts to get <<< to end in a newline have worked.
>>
>> What were these attempts?
>>
>> (I get newlines with all the prominent shells; ksh, bash, zsh.)
>
> (Using bash)
I don't think that your examples show what you say to be happening...
> The only newline is generated by echo:
>
> $ read xx <<< qwerty
> $ echo "$xx"
> qwerty
> $
Above 'read' is removing the newlines, also the one added by '<<<'.
(Try read xx <<< $'qwerty\n\n' to see the behaviour.)
Then try non-read contexts...
$ od -c <<< qwerty
0000000 q w e r t y \n
0000007
$ awk 'BEGIN{FS=RS="x"}{print length()}' <<< "foo"
4
>
> The trailing newline is stripped:
In subshells (your examples below) multiple trailing newlines are stripped,
and finally one added by <<< (as I understand it).
> $ read xx <<< "$(pwd)"
> $ echo "$xx"
> /home/chris/Downloads
> $
>
> It even strips trailing newlines:
>
> $ cat <<< "$(printf '%s\n\n' qwerty)"
> qwerty
> $
>
> But not embedded newlines:
>
> $ cat <<< "$(printf '\n%s\n\n' qwe$'\n'rty)"
>
> qwe
> rty
> $
I want to add an Email from David Korn posted 10 years ago in the AST
mailling list, where the basic question was raised and where he gave a
very terse answer...
David Korn
Mi, 19.05.2010 15:37
Subject: Re: RE: [ast-users] wc --bytes <<<"foo" == 4?
--------
> >
> > Is this a bug or a feature that wc --bytes <<<"foo" returns 4? Does
> > the <<<string operator add a new line at the end by default?
>
> Apparently.
>
> $ od -c <<<"foo"
> 0000000 f o o \n
> 0000004
This is intentional.
When the <<< had been introduced in Ksh I racall to have had a discussion
with David Korn about the originally implemented behaviour at that time.
I cannot find those Emails now but as far as memory serves the original
implementation did not show the newline, then it was added to behave -
YMMV - consistent. (Awaiting your posts or emails of complaints, but the
added newline became the de facto standard it seems.)
Janis