bug-bash
[Top][All Lists]
Advanced

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

Re: document that read built-in can't return zero-length string in the m


From: Robert Elz
Subject: Re: document that read built-in can't return zero-length string in the middle of input
Date: Sat, 13 Jan 2024 02:06:08 +0700

    Date:        Fri, 12 Jan 2024 07:15:35 -0500
    From:        Greg Wooledge <greg@wooledge.org>
    Message-ID:  <ZaEtZzH6SkBCkxvj@wooledge.org>

  | This was one of the things I tested:

Perhaps intended to, but didn't, or not in this example:

  | { read -N1; read -r b c; } < <(printf \\nabc); declare -p REPLY a b c

Rewrite that, correctly for the purpose, as:

    { read -N1; read -r b c; } < <(printf \\\\\\nabc); declare -p REPLY b c

or a bit more cleanly as:

    { read -N1; read -r b c; } < <(printf '\\\nabc'); declare -p REPLY b c

and (both cases) you get:

declare -- REPLY="a"
declare -- b="bc"
declare -- c=""

bash is (as I assumed earlier it would) doing the correct thing, and
ignoring the elided newline completely.   If it didn't, that would have
been a bug, but it does, so all is well.

In the case you tested, printf output $'\nabc' and read -N1 correctly
read the first char (\n) into REPLY (and then read abc into b, 'c' isn't
really needed for this example).

[Note: I deleted the output of 'a' as I didn't conveniently have a
variable called 'a' defined, so declare issued an error, which would
have just confused things.]

kre

ps: the use of process substitution there is just silly, it would work
just as well, and be easier to understand if written:

        printf '\\\nabc' | { read -N1; read -r b c; }; declare -p REPLY b c
or      { read -N1; read -r b c; } <<< $'\\\nabc' ; declare -p REPLY b c

I much prefer the former of those two.





reply via email to

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