bug-bash
[Top][All Lists]
Advanced

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

Re: Trailing newlines disappear


From: Chet Ramey
Subject: Re: Trailing newlines disappear
Date: Fri, 9 Jun 2017 09:22:58 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.1.1

On 6/9/17 8:06 AM, Greg Wooledge wrote:

> Actually, it looks like the mapfile variant also has its own... let's
> call it "idiosyncratic" NUL handling:
> 
> imadev:~$ unset a
> imadev:~$ mapfile -t a < <(printf 'foo\0bar\nbaz\nquux\n')
> imadev:~$ declare -p a
> declare -a a=([0]="foo" [1]="baz" [2]="quux")
> 
> Where did "bar" go?  Looks like mapfile loses the contents after a NUL
> byte within each line, but then subsequent lines are fine.  (E.g. what
> you would get if you blindly called strcpy() for each line, with the
> source buffer possibly having multiple NULs.)

There's no mystery here. Mapfile reads lines delimited by newlines --
read(2) is happy to do that job. Shell variable values can't contain NULs;
they never have been able to. The entire line read gets stored as the
value, but since C strings are NULL-terminated, you just see the contents
preceding the first NUL. Should mapfile silently drop the NULs?

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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