I am trying to echo user, shell and home fields from /etc/passwd file using a for loop. What I am trying is to use:
read -r user pass desc home shell
In this case you can't use a
for-loop as you don't know how much input you will get. A
for-loop is "static" in the sense that the shell needs to know exactly what it's iterating over when it gets to it.
You could bypass this through actually reading in the whole file (and in that way get to know exactly what to iterate over) and then
for-looping over that data, but it would be rather clunky.
The "correct" way is to
read in a
IFS=':' while read -r user pass uid gid desc home shell; do echo "User is '$user', has shell '$shell'" done </etc/passwd