Reading process output

As a compound command, a while loop can also accept the output of another command as input, using a pipe. For example, to pass the output of who -T to the loop as input, to print only the terminal names for active users on the system, we might write:

who -T | while read -r username state terminal _ ; do
    printf '%s\n' "$terminal"
done

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.