Search code examples
bashstdin

Why is **read** ignoring stdin


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

Solution

  • 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]}" )