The way to do this with standard input is by passing the -a
flag to read:
read -a words
echo "${words[@]}"
This will read your entire line into an indexed array variable, in this case named words. You can then perform any array operations you like on words with shell parameter expansions.
For file-oriented operations, current versions of Bash also support the mapfile built-in. For example:
mapfile < /etc/passwd
echo ${MAPFILE[0]}
Either way, arrays are the way to go. It's worth your time to familiarize yourself with Bash array syntax to make the most of this feature.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…