Fortran Programming Guide

Command-Line I/O Redirection and Piping

Another way to associate a physical file with a program's logical unit number is by redirecting or piping the preconnected standard I/O files. Redirection or piping occurs on the runtime execution command.

In this way, a program that reads standard input (unit 5) and writes to standard output (unit 6) or standard error (unit 0) can, by redirection (using <, >, >>, >&, |, |&, 2>, 2>&1 on the command line), read or write to any other named file.

This is shown in the following table:

Table 2-1 csh/sh/ksh Redirection and Piping on the Command Line

Action 

Using C Shell 

Using Bourne or Korn Shell 

Standard input --read from mydata 

myprog < mydata myprog < mydata

Standard output --write (overwrite) myoutput 

myprog > myoutput myprog > myoutput

Standard output -- write/append to myoutput 

myprog >> myoutput myprog >> myoutput

Redirect standard error to a file 

myprog >& errorfile myprog 2> errorfile

Pipe standard output to input of another program 

myprog1 | myprog2 myprog1 | myprog2

Pipe standard error and output to another program 

myprog1 |& myprog2 myprog1 2>&1 | myprog2

See the csh, ksh,and sh man pages for details on redirection and piping on the command line.