bug-bash
[Top][All Lists]
Advanced

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

Re: Trailing newlines disappear


From: Greg Wooledge
Subject: Re: Trailing newlines disappear
Date: Fri, 9 Jun 2017 08:06:42 -0400
User-agent: Mutt/1.4.2.3i

On Fri, Jun 09, 2017 at 08:06:28AM +0700, Peter & Kelly Passchier wrote:
> On 09/06/2560 05:26, Eduardo Bustamante wrote:
> > What's wrong with:
> > 
> > IFS= read -rd '' foo < "$file"
> 
> I think that's the winner!

So long as you know the file doesn't contain any NUL bytes.  The other
variants will silently or loudly strip them out, while this one stops
reading at the first one it sees.

imadev:~$ unset a
imadev:~$ a=$(printf 'foo\0bar\nbaz\nquux\n'; printf x) a=${a%x}
bash: warning: command substitution: ignored null byte in input
imadev:~$ declare -p a
declare -- a="foobar
baz
quux
"

imadev:~$ unset a
imadev:~$ IFS= read -rd '' a < <(printf 'foo\0bar\nbaz\nquux\n')
imadev:~$ declare -p a
declare -- a="foo"

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.)



reply via email to

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