tags:

views:

226

answers:

3

Here's what I'm trying. What I want is the last echo to say "one two three four test1..." as it loops. It's not working; read line is coming up empty. Is there something subtle here or is this just not going to work?

array=( one two three )
echo ${array[@]}
#one two three
array=( ${array[@]} four )
echo ${array[@]}
#one two three four


while read line; do
        array=( ${array[@]} $line )
        echo ${array[@]}
done < <( echo <<EOM
test1
test2
test3
test4
EOM
)
+3  A: 

I would normally write:

while read line
do
    array=( ${array[@]} $line )
    echo ${array[@]}
done <<EOM
test1
test2
test3
test4
EOM

Or, even more likely:

cat <<EOF |
test1
test2
test3
test4
EOF

while read line
do
    array=( ${array[@]} $line )
    echo ${array[@]}
done
Jonathan Leffler
+2  A: 

replace

done < <( echo <<EOM

with

done < <(cat << EOM

Worked for me.

sha
A: 

You can put the command in front of while instead:

(echo <<EOM
test1
test2
test3
test4
EOM
) | while read line; do
        array=( ${array[@]} $line )
        echo ${array[@]}
done
hlovdal
And as sha says, cat is probably more appropriate than echo here.
hlovdal