On Sat, Apr 1, 2023, at 9:27 PM, Kerin Millar wrote:
> On Sat, 1 Apr 2023 19:44:10 -0400
> Saint Michael <[email protected]> wrote:
>
>> There is an additional problem with IFS and the command read
>>
>> Suppose I have variable $line with a string "a,b,c,d"
>> IFS=',' read -r x1 <<< $line
>> Bash will assign the whole line to x1
>> echo $x1
>> line="a,b,c,d";IFS=',' read -r x1 <<< $line;echo $x1;
>> a,b,c,d
>> but if I use two variables
>> line="a,b,c,d";IFS=',' read -r x1 x2 <<< $line;echo "$x1 ---> $x2";
>> a ---> b,c,d
>> this is incorrect. If IFS=",", then a read -r statement must assign the
>
> No it isn't.
>
>> first value to the single variable, and disregard the rest.
>
> No it musn't. Read
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html
> and pay particular attention to the definition of what must happen
> where there are fewer vars (names) than fields encountered.
Also, observe the behavior of other shells:
% cat foo.sh
echo a,b,c,d | {
IFS=, read x1
printf '%s\n' "$x1"
}
echo a,b,c,d | {
IFS=, read x1 x2
printf '%s ---> %s\n' "$x1" "$x2"
}
% bash foo.sh
a,b,c,d
a ---> b,c,d
% dash foo.sh
a,b,c,d
a ---> b,c,d
% ksh foo.sh
a,b,c,d
a ---> b,c,d
% mksh foo.sh
a,b,c,d
a ---> b,c,d
% yash foo.sh
a,b,c,d
a ---> b,c,d
% zsh foo.sh
a,b,c,d
a ---> b,c,d
And the Heirloom Bourne shell:
<larryv> b# echo a,b,c,d | { IFS=, read x1; printf '%s\n' "$x1";
}; echo a,b,c,d | { IFS=, read x1 x2; printf '%s ---> %s\n' "$x1" "$x2"; }
<shbot> a,b,c,d
<shbot> a ---> b,c,d
--
vq