Skip to main content

1.11 Job redirection

Most jobs need input and output options. By convention in linux, the default input is the standard input and the default output is the standard output. The standard input is defined as the input from the prompt at your terminal. Similarly, the standard output is defined as the output to your prompt at your terminal. It would be a huge pain if you could only use standard input and standard output - imagine a program that needs 10,000 numbers as input - you wouldn't want to feed them in by hand! Instead, under Linux, there is the oportunity to redirect input and output. Input redirection is indicated by a < in front of the input file name, and output redirection is indicated by a > in front of a file name.

Another useful redirection is >> . This will append output to a file, rather than write over that file with the output.


Suppose you have a program called averages, and the data you wish to average is collected in a file called ifile. Let's say you also would like to put your output into a file called ofile for safekeeping. You'd also like to put this job into the background. You can then run this job from the prompt by typing
 ./averages < ifile > ofile & 

Another useful redirection tool is the pipe, invoked using the vertical bar symbol |. By using a pipe, you can direct the standard output of one process into the standard input of another process. The general syntax is

proc1 | proc 2

After you've encountered a few basic Linux commands, we'll take another look at the pipe command.