Choosing the splitting character

The read builtin checks a special variable named IFS  the Internal Field Separator  to decide where to split the line it reads. If this variable is unset, as it has been for our examples so far, it splits on groups of spaces and tabs.

If our data is separated by some other character, for example by colons in /etc/passwd, we can set IFS as an environment variable for read to influence its behavior. For example, to print every numeric user ID from the file without using cut, we could do this:

while IFS=: read -r user pass uid gid gecos home shell ; do
    printf '%s\n' "$uid"
done < /etc/passwd

Notice that the setting for IFS to the colon character, :, occurs just before the read command, and there is no control operator between them. Recall from Chapter 5, Variables and Patterns, that this sets IFS as an environment variable for the read command, and only for the read command.

The IFS variable affects other word-splitting behavior outside of read too, but this is not often needed, and is something best avoided for beginners. If you need to change IFS, do it using the environment variable prefix trick if you can, to avoid unwanted side-effects.