bug-bash
[Top][All Lists]
Advanced

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

Re: Builtin 'read' data not saved


From: Davide Brini
Subject: Re: Builtin 'read' data not saved
Date: Thu, 2 Jan 2014 23:57:44 +0100

On Thu, 2 Jan 2014 11:35:11 -0800, "P Fudd" <address@hidden> wrote:

> Here's some more oddities:

Ok, the link I sent you is more about the issue you describe in your first
message. See below for more on the new ones in this message.

> =====failing.sh:
> #!/bin/bash
> R="1|2"
> IFS='|' read -r A B <<< $R
> echo A=$A, B=$B
> ====
> Expected: "A=1, B=2"
> Actual: "A=1 2, B="

Here $R is expanded and wordsplitted using IFS before read is executed,
which means that $R does *not* contain any pipe symbol at the time "read"
is executed, so $A gets the whole thing.

> ====fail2.sh:
> #!/bin/bash
> R="1|2"
> while IFS='|' read -r A B; do
> echo 1:A=$A, B=$B
> done <<< $R
> echo 2:A=$A, B=$B
> ====
> Expected:
>   1:A=1, B=2
>   2:A=1, B=2
> Actual:
>   1:A=1, B=2
>   2:A=, B=

This is slightly different, as when $R is expanded, IFS is still the
default, so it's expanded to "1|2" as expected, and the alternate IFS is
only in effect during the while/read loop. As expected, after the first
loop iteration $A is 1 and $B is 2. However read is executed once more, and
that second execution reads nothing, thus obviously setting both A and B
empty. Run with "set -x" and you'll see it yourself.

-- 
D.



reply via email to

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