Standard Input and Standard Output

It may sound complicated, but it isn't. Standard in (STDIN) is simply where the system expects to find its input. This is usually the keyboard, although it can be a program or shell script. When you change that default, you call it redirecting from STDIN.

Similarly, standard out (STDOUT) is where the system expects to direct its output, usually the terminal screen. Again, redirection of STDOUT is at the discretion of whatever command or script is executing at the time. The chain of events from STDIN to STDOUT looks something like this:

 standard in -> Linux command -> standard out 

STDIN is often referred to as fd0, or file descriptor 0, and STDOUT is usually thought of as fd1. There is also standard error (STDERR), where the system reports any errors in program execution. By default, this is also the terminal. To redirect STDOUT, use the greater-than sign (>). As you might have guessed, to redirect from STDIN, you use the less-than sign (<). But what exactly does that mean? Let's try an experiment. Randomly search your brain and pick a handful of names. Got them? Good. Now type the cat command and redirect its STDOUT to a file called random_names.

 cat > random_names 

Your cursor will just sit there and wait for you to do something, so type those names, pressing <Enter> after each one. What's happening here is that cat is taking its input from STDIN and writing it out to your new file. When you are done with your list of names, press <Ctrl+D> to finish. <Ctrl+D>, by the way, stands for EOF, or end of file.

 Marie Curie Albert Einstein Mark Twain Wolfgang Amadeus Mozart Stephen Hawking Hedy Lamarr ^D 

If you cat this file (cat random_names), the names will be written to STDOUT in this case, your terminal window. You can also give cat several files at the same time. For instance, you could do something like this:

 cat file1 file2 file3 

Each file would be listed one right after the other. That output could then be redirected into another file. You could also have it print out the same file over and over (cat random_names random_names random_names). cat isn't fussy about these things and will deal with binary files (programs) just as quickly. Beware of using cat to print out the contents of a program to your terminal screen. At worst, your shell session will lock up or reward you with a lot of beeping and weird characters.

Quick Tip


If you do get caught in such a situation and all the characters on your screen appear as junk, try typing echo, then pressing <Ctrl+v> and <Ctrl+o>. If you can still type, you can also try typing stty sane, then pressing <Ctrl+j>.

Redirecting STDIN works pretty much the same way, except that you use the less-than sign instead. Using the sort command, let's take that file of random names and work with it. Many commands that work with files can take their input directly from that file. Unless told otherwise, cat and sort will think that the word following the command is a file name. That's why you did the STDIN redirection thing. Yes, that's right: STDIN is just another file. Sort of.

 sort random_names 

The result, of course, is that you get all your names printed out in alphabetical order. You could have also specified that sort take its input from a redirected STDIN. It looks a bit strange, but this is perfectly valid.

 [mgagne@testsys tmp]$ sort < random_names Albert Einstein Hedy Lamarr Marie Curie Mark Twain Stephen Hawking Wolfgang Amadeus Mozart 

One more variation involves defining your STDIN (as you did previously) and specifying a different STDOUT all on the same line. In the following example, I am redirecting from my file and redirecting that output to a new file called sorted_names.

 sort < random_names > sorted_names 

Moving to Linux(c) Kiss the Blue Screen of Death Goodbye!
Moving to Linux: Kiss the Blue Screen of Death Goodbye!
ISBN: 0321159985
EAN: 2147483647
Year: 2005
Pages: 181 © 2008-2017.
If you may any questions please contact us: