Re: 'read' sometimes truncates trailing field delimiter
From:
Greg Wooledge
Subject:
Re: 'read' sometimes truncates trailing field delimiter
Date:
Sun, 12 Jan 2025 16:19:26 -0500
User-agent:
Mutt/1.10.1 (2018-07-13)
On Sun, Jan 12, 2025 at 16:10:03 -0500, Chet Ramey wrote:
> This is a combination of two rules: characters in IFS are field
> terminators, not separators, and when `read' is supplied fewer
> variables than fields,
>
> "Any remaining input in the original field being processed shall be
> returned to the read utility "unsplit"; that is, unmodified except that any
> leading or trailing IFS white space, as defined in 2.6.5 Field Splitting
> shall be removed."
>
> Which basically means that the last variable gets everything else on
> the line, including non-whitespace IFS characters.
Part of the pitfall is that this isn't always true.
In this case, we get the "expected" result:
hobbit:~$ IFS=, read -r field1 rest <<< 'ab,cd,ef,'
hobbit:~$ declare -p field1 rest
declare -- field1="ab"
declare -- rest="cd,ef,"
In this case, we don't:
hobbit:~$ IFS=, read -r field1 rest <<< 'ab,ef,'
hobbit:~$ declare -p field1 rest
declare -- field1="ab"
declare -- rest="ef"