To offer an awk
alternative to soronta's helpful and well-explained answer:
IFS=$'\n' read -d '' -ra usersAndHomeDirs < \
<(awk -F':' '$3 >= 10 && $3 != 65534 { print $1, $6 }' /etc/passwd)
-d ''
tells read
to read the entire input as a whole,
IFS=$'\n'
tells read
to split the input into lines, and store them in elements of array (-a
) usersAndHomeDir
(-r
turns off \
interpretation in the input, and in most cases should be added by default).
< <(awk ..)
then uses a process substitution (<(...)
) to make awk
output "<user> <home-dir>"
lines, and feeds them to read
via stdin (<
).
Note that each element of "${usersAndHomeDirs[@]}"
is now a single "<user> <home-dir>"
string, which you'll have to split into its components later.
(Bash doesn't offer a way to fill two arrays in parallel.)
To use something other than a space as the separator, pass -v OFS=<separator-string>
to the awk
command.
Letting awk
process the input file will be much faster in general than doing it in a shell loop, though in this particular case it may not make much of a difference, if your /etc/passwd
file is smallish.