Re: mapfile question: Is there a better way to avoid the trailing newlin
From:
Stan Marsh
Subject:
Re: mapfile question: Is there a better way to avoid the trailing newline?
Date:
Wed, 01 Oct 2025 13:15:41 -0600
>> --- Cut Here ---
>> $ foo=$'this is a\ttest of this\tthat one'
>> $ declare -p foo
>> declare -- foo=$'this is a\ttest of this\tthat one'
>> $ mapfile -t -d $'\t' <<< "$foo"; declare -p MAPFILE
>> declare -a MAPFILE=([0]="this is a" [1]="test of this" [2]=$'that one\n')
>> $
>> --- Cut Here ---
>>
>> Note the trailing newline on the last element of MAPFILE.
>This looks like an X-Y question, honestly. You're reading a tab-delimited
>stream, but you're using <<< to feed that stream to your reader, and
>the <<< is adding a newline.
It actually is not an X-Y problem.
My data really is: field<tab>field<tab>field
And I really do want to use mapfile to split it up.
>In your REAL application, what's your data source? Does it have an
>extra newline on the end?
No, it isn't.
>Is it missing the final tab, as in your example?
No, it isn't.
Though I see your point. I could fix it by jamming a final tab in there,
at the cost of creating an extra blank array element. Which as it turns
out would be OK in my application, but is unclean in general.
>If you need a reproducer-example that doesn't artificially add newlines
>to the stream, use <() instead.
> mapfile -t -d $'\t' < <(printf %s "$data")
That might work, but, honestly, it's more trouble than just trimming
the newline.
Anyway, it is no big deal. I just thought I'd ask and see if there's
something obvious that I'm missing. Oh, and also, I am not interested
in any solutions based on IFS.