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 ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.