Standard output
From Linux 101, The beginner's guide to all things Linux.
There are several very important text streams associated to each terminal. They are standard input, standard output, and standard error.
Standard input, or stdin, is simply your stream of characters that you type in on your keyboard that are to be sent to the program.
Standard output, or stdout, is the name of the default output stream that all console applications have. This output is sent by default directly to the console, and is the program's output. However, it can be redirected elsewhere if desired. For example, the
-
ls
command by itself will display the list of files in the current directory. If you wanted to write this list to a file, however, you could redirect the standard output to a file like this:
-
ls > filelist.txt
Note that > causes the file to be overwritten. If you want to append to the file rather than overwrite it, you can do:
-
ls >> filelist.txt
Standard error, or stderr, is another output stream. When your program is reporting an error, it should be using this stream. To the user, there is no difference in how stdout and stderr are displayed, but it will affect how the console handles things with the > stream operators.
Some examples:
-
program > ~/somelogfile.log 2>&1
|
Note: $ program &> ~/somelogfile.log is a Bash-specific shortcut equivalent to $ program > ~/somelogfile.log 2>&1 |
This tells the bash shell to execute "program" and send the stdout to "~/somelogfile.log" and send the stderr (2) to the same place as stdout (&1), which is being sent to the file "~/somelogfile.log". The 2>&1 may look a little bizarre. This isn't a common construction outside of shell scripting, but this will give you some hints if you see it again elsewhere.
It is important to realize that order the redirections occur in is important. For example, if you had:
-
program 2>&1 > ~/somelogfile.log
stderr would get sent to where stdout is currently sent (i.e. the screen) and then stdout would get sent to "~/somelogfile.log". The resulting behavior would be, things printed to stderr would get printed to the screen, and things printed to stdout would get printed to the file.
Programs can also create other streams for input or output -- this is how applications interact with files.
The terminology on this page is very common and will be used by programmers among many languages.

