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

Re: Bash different behaviour of read / strings in jessie versus stretch (regression?)



On Sat, Jan 07, 2017 at 09:54:34PM +0100, foo fighter wrote:
> In jessie "version 1" and "version 2" behave identical. In stretch, "version 2" behaves different (only one item in array)

> echo "===========================Version 2==========================="
[...]
> _inputstring="$(echo -e "item01\nitem02\nitem03")"

_inputstring=$'item01\nitem02\nitem03'

> read -ra _array <<<$(echo "$_inputstring") # <<< WITH -"- around string

This is just wrong.  You're using $(echo) which is silly and pointless,
and you've failed to quote the result, and you've failed to tell read
not to stop reading at the first newline, and you've failed to set IFS
to use only newlines as delimiters between array elements.

The only way yours "works" at all is because two of your errors are
canceling each other.  The unquoted $(echo) is doing word splitting
and then reassembling the result, effectively replacing each newline
with a space, so that you only have a single line of input, which is
why read sees the entire input....  Yikes.

Really, you should be using mapfile (a.k.a. readarray) when reading
a multi-line input where each line is intended to become one array
element.

_inputstring=$'item01\nitem02\nitem03'
mapfile -t _array <<< "$_inputstring"
declare -p _array

Try that in both versions of bash, and I believe that it will work
consistently and correctly.

Now I get to start guessing.  My first guess is that you are doing
something like this:

tmp=$(some program that writes multiple lines)
read -ra array <<< $(echo "tmp")  # wrong

Just do this instead:

mapfile -t array < <(some program that writes multiple lines)


Reply to: