The Unix Plumber's Helper (Pipes) and Electrician's Apprentice Guide (Switches)
Pipes and the Flow of Information
Unix commands all share a standard method of receiving and giving information. These are referred to as standard in (STDIN) and standard out (STDOUT). In many cases, STDIN is the keyboard and STDOUT is the screen. The login shell you use is a very basic example of this. It takes in what you type on the keyboard gives you information back on the screen. Other commands might take a filename from which they would get STDIN. Commands which type information to the screen, and that's just about all of them, are using STDOUT.
This may seem strange but it gives you a very interesting opportunity. Imagine being able to connect STDOUT from one command with STDIN from another! This is perfectly possible in Unix. The device you use is called a "pipe" and it is written with the symbol "|" (that's the vertical bar, often found on the keyboard as shift-\). Let's look at a helpful example. Suppose you want to print on a printer and not on the screen, a list of all the files in your directory. You probably know the "ls" command which will list your directory and the "lp" command which will print files. Neither one alone will do what you want. Why not connect STDOUT from the "ls" command to STDIN on the "lp" command? Here's how that would look:
You are not limited to one pipe at a time. You can continue piping the output of one command to the input of another command as long as it makes any sense. For example, take the output of the spell command run on a file called "my_file" (i.e. misspelled words), sort them in reverse alphabetical order and look at them with the page command. This may not make much sense but it would look like this:
spell my_file | sort -r | page
In the end, you don't have to let STDOUT come to the screen. It can be redirected to a file. For that matter, STDIN doesnt always have to begin with the keyboard. Here are a few examples of redirection. First, lets take the output of " ls " and save it to a file called "save_output."
ls > save_output
We can redo our "spell" example and save the results to a file called "new_file" instead of viewing them like this:
spell my_file | sort -r > new_file
Taking STDIN from a file instead of the keyboard can be fairly powerful. Since sending output to a file requires ">" you might guess that taking STDIN from a file requires "<". One nice use of this redirect is that of mailing an existing file. Normally if you type
mail user@machine.name
the mail program will continue to take input from the keyboard. You would proceed to type your email message and the ctrl-d to finish and send the message. If you wanted to send a file instead of inputting the message by hand you could type
mail user@machine.name < existing_file_name
Using Switches on Commands
Almost all Unix commands have options which change they way they work. These are known as "switches." They are given on the command line after the command and before any filenames are given. Switches are always indicated with a hyphen with only one command taking an exception; the "tar" command. Switches don't have to be given separately, they can be put together with a single hyphen. For instance, one of my favorite options let's me do a directory listing in reverse time order. With this, I can always find the last file I was working on, since I always forget what I called it. The options are "t" for time-ordered, "r" for reverse, and "l" for a long listing which makes the output clearer. The command looks like this:
ls -lrt
Equally valid would have been
ls -l -r -t
but it sure takes longer to type. There are some switches that expect more information such as the destination option for "lp". The option sets where the output should be directed, and looks like this.
lp -d printer_name filename
where "printer_name" is a valid printer on the UofA network.
© 1998, The University of Alabama. The information included here is for the University of Alabama central computing facility as it was configured on the document date. It may or may not apply to other Unix systems.

