As you will recall, the file users that was created previously contains a list of all the users currently logged in to the system. Because you know that there will be one line in the file for each user logged in to the system, you can easily determine the number of users logged in by simply counting the number of lines in the users file:
$ who > users
$ wc -l < users
This output would indicate that currently five users were logged in. Now you have a command sequence you can use whenever you want to know how many users are logged in.
Another approach to determine the number of logged-in users bypasses the use of a file. The Unix system allows you to effectively connect two commands together. This connection is known as a pipe, and it enables you to take the output from one command and feed it directly into the input of another command. A pipe is effected by the character |, which is placed between the two commands. So to make a pipe between the who and wc -l commands, you simply type who | wc -l:
$ who | wc -l
The pipe that is effected between these two commands is depicted in figure below:
Figure 2.12. Pipeline process: who | wc -l.
When a pipe is set up between two commands, the standard output from the first command is connected directly to the standard input of the second command. You know that the who command writes its list of logged-in users to standard output. Furthermore, you know that if no filename argument is specified to the wc command, it takes its input from standard input. Therefore, the list of logged-in users that is output from the who command automatically becomes the input to the wc command. Note that you never see the output of the who command at the terminal because it is piped directly into the wc command.:
Figure 2.13. Pipeline process.
A pipe can be made between any two programs, provided that the first program writes its output to standard output, and the second program reads its input from standard input.
As another example of a pipe, suppose that you wanted to count the number of files contained in your directory. Knowledge of the fact that the ls command displays one line of output per file enables you to use the same type of approach as before:
$ ls | wc -l
The output indicates that the current directory contains 10 files.
It is also possible to form a pipeline consisting of more than two programs, with the output of one program feeding into the input of the next.
The term filter is often used in Unix terminology to refer to any program that can take input from standard input, perform some operation on that input, and write the results to standard output. More succinctly, a filter is any program that can be used between two other programs in a pipeline. So in the previous pipeline, wc is considered a filter. ls is not because it does not read its input from standard input. As other examples, cat and sort are filters, whereas who, date, cd, pwd, echo, rm, mv, and cp are not.