Take the 2-minute tour ×

From http://www.cplusplus.com/reference/iostream/cout/:

By default, most systems have their standard output set to the console, where text messages are shown, although this can generally be redirected.

I've never heard of a system where stdout is anything other than a console window, by default or otherwise. I can see how redirecting it might be beneficial in systems where printing is an expensive operation, but that shouldn't be an issue in modern computers, right?

share|improve this question
On the command line I can do type in something like program.exe > output.txt and everything on stdout will be written to output.txt instead of the console. CGI scripts work this way too; the program writes out the HTML on stdout and the web server delivers the output down the HTTP connection. –  In silico Jun 23 '11 at 0:03
Also, inetd will connect stdin and stdout to a socket. –  Dietrich Epp Jun 23 '11 at 0:10
add comment

7 Answers

up vote 8 down vote accepted

On most systems you can redirect the standard input/output/error to other file descriptors or locations.

For example (on Unix):

./appname > output

Redirects the stdout from appname to a file named output.

./appname 2> errors > output

Redirects stdout to a file named output, and all errors from stderr to a file named errors.

On unix systems you can also have a program open a file descriptor and point it at stdin, such as this:

echo "input" > input
cat input | ./appname

This will cause the program to read from the pipe for stdin.

This is how in unix you can "pipe" various different utilities together to create one larger tool.

find . -type f | ./appname | grep -iv "search"

This will run the find command, and take its output and pipe it into ./appname, then appname's output will be sent to grep's input which then searches for the word "search", displaying just the results that match.

It allows many small utilities to have a very powerful effect.

Think of the >, <, and | like plumbing.

> is like the drain in a sink, it accepts data and stores it where you want to put it. When a shell encounters the > it will open a file.

> file

When the shell sees the above, it will open the file using a standard system call, and remember that file descriptor. In the above case since there is no input it will create an empty file and allow you to type more commands.

banner Hello

This command writes Hello in really big letters to the console, and will cause it to scroll (I am using Unix here since it is what I know best). The output is simply written to standard out. Using a "sink" (>) we can control where the output goes, so

banner Hello > bannerout

will cause all of the data from banner's standard output to be redirected to the file descriptor the shell has opened and thus be written to a file named bannerout.

Pipes work similarly to >'s in that they help control the flow of where the data goes. Pipes however can't write to files, and can only be used to help the flow of data go from one point to another.

For example, here is water flowing through several substations and waste cleaning:

pump --from lake | treatment --cleanse-water | pump | reservoir | pump > glass

The water flows from the lake, through a pipe to the water treatment plant, from the plant back into a pump that moves it to a reservoir, then it is pumped once more into the municipal water pipes and through your sink into your glass.

Notice that the pipes simply connect all of the outputs together, ultimately it ends up in your glass.

It is the same way with commands and processing them in a shell on Linux. It also follows a path to get to an end result.

Now there is one final thing that I hadn't discussed yet in my previous statements, that is the < input character. What it does is read from a file and output it to stdin on programs.

cat < bannerout

Will simply print what was stored in bannerout. This can be used if you have a file you want to process, but don't want to prepend cat <file> because of not wanting to run an extra command in the chain.

So try this:

echo "Hello" > bannerinput
banner < bannerinput

This will first put the string "Hello" in the file bannerinput, and then when your run banner it will read from the file bannerinput.

I hope this helps you understand how redirection and pipping works on Unix (some if not most will apply to Windows as well).

share|improve this answer
I'm not sure I understand what the difference is between the method with > and the method with |. –  Maxpm Jun 23 '11 at 0:27
The > is a redirect symbol, it is like a drain in a sink, it takes everything and puts it in the file mentioned. Whereas a | is a pipe. It allows data to flow from the output of one process to the input of another process. –  X-Istence Jun 23 '11 at 0:38
add comment

Of course it could be. I may want to redirect standard out to a text file, another process, a socket, whatever.

By default it is the Console, but the are a variety of reasons to redirect it, the most useful (in step with the Unix philosophy) being the redirection of the output of one program to the input of another program. This allows one to create many small, lightweight programs that feed into one another and work as discrete parts of a larger system.

Basically, it's just a simple yet powerful mechanism for sharing data. It is more popular on *nix systems for the reason I mention above, but it applies to Windows as well.

share|improve this answer
add comment

So far all of the answers have been in the context of the thing (shell, whatever) that invokes the program. The program itself can make stdout something other than the terminal. The C standard library provides freopen which lets the programmer redirect stdout in any compliant environment. POSIX provides a number of other mechanisms (popen, fdopen, ...) that gives the programmer even more control. I suspect Windows provides analogous mechanisms.

share|improve this answer
add comment

Any number of things can happen to the three standard file descriptors 0, 1 and 2. Anyone can launch a new process with the file descriptors attached to anything they like.

For instance, GNU screen puts the output into a pipe and allows dynamic reattaching of a session. SSH takes the output and returns it to the other end. And of course all the numerous shell redirectors regularly make use of manipulating the file descriptors.

share|improve this answer
add comment

For a program to have stdout it must be running on a hosted implementation (one with an Operating System), or on a free-standing implementation with extras.

I'm having a hard time figuring such an implementation without a console of some kind, but let's suppose for a moment that Mars Rover has a full OS and is programmed in C (or C++) and doesn't have that console

/* 2001-07-15: JPL: stdout is the headquarters */
puts("Help. I'm stuck.");

might have sent the message to NASA headquarters.

share|improve this answer
add comment

Both Windows and Linux will redirect stdout to a file if you run the program like this:

my_program > some_file

This is the most common case, but many other types of redirection are possible. On Linux, you can redirect stdout to anything that supports the "file descriptor" interface, such as a pipe, socket, file, and various other things.

share|improve this answer
add comment

One simple example of a case where one might want to redirect stdout is when passing the information to another program. The Unix/Linux command ps generates a list of processes that belong to the current user. If this list was long and you wanted to search for a particular process, you could enter

ps | grep thing

which would redirect the stdout of ps to the stdin of grep thing.

share|improve this answer
add comment

Your Answer


