0

Why manually inputing values as an array using read command works :

read -a words
## type values here and then enter

But this does not :

printf "uno\tdos\n" | read -a spanishWords
echo "${spanishWords[0]}" ## This is empty
Matias Barrios
  • 3,700
  • 2
  • 12
  • 33

1 Answers1

2

They both work just fine. The problem is that your second example calls read in a separate process. In that separate process, spanishWords contains the correct contents. But that doesn't help you.

This would work:

printf "uno\tdos\n" |
   ( read -a spanishWords;
     echo "${spanishWords[0]}" )
David Schwartz
  • 166,415
  • 16
  • 184
  • 259