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
  • 4,674
  • 3
  • 22
  • 49

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
  • 179,497
  • 17
  • 214
  • 278