Previous Table of Contents Next


Module 67
INPUT/OUTPUT AND REDIRECTION

DESCRIPTION

Many UNIX commands read from the standard input, write to the standard output, and report errors to the standard error. When you run a command it opens the standard input, output, and error automatically. The standard input, by default, is your keyboard; the standard output and error is your terminal screen. Thus a command reads from your keyboard if it reads the standard input, and writes to your terminal screen if it writes to the standard output or error.

These three standard I/O files (devices) are assigned file descriptors. File descriptors are numbers that relate to an open file. If a command opens another file for reading or writing, the next available file descriptor is assigned to the file. The following table describes each I/O type and the related default file descriptor.

STANDARD INPUT/OUTPUT/ERROR

Conventional Names Default Device Input or
Output is Connected to
File Descriptor (Used by the Shell)

Standard Input Received from terminal keyboard (or from a pipe or file) 0
Standard Output Sent to the terminal screen (or to a pipe or file) 1
Standard Error Sent to the terminal screen (or to a pipe or file) 2

REDIRECTION

The shell allows redirection of the standard input, output, and error of a command. Redirection of I/O is the capability to change the source of the input and the destination of the output. This is done by using the greater than and less than signs (<,>) to guide the output. The redirection symbols(<,>) are not passed to the command as arguments; they are interpreted by the shell. The symbols may appear anywhere on a simple-command line or may precede or follow a command. The pipe command (|) also redirects input and output between programs. It is not considered a redirection symbol because it is used between commands. Redirection symbols affect the source input or the destination output of a command.


CAUTION:  
You should be careful when redirecting I/O. It is possible to overwrite files or lock up your terminal. If you overwrite a file, your system administrator may be able to recover it from the last backup. If you lock up your terminal, contact your system administrator for assistance.




NOTE:  
A single digit may precede any of the redirection symbols to change the default file descriptor that is affected.



GENERAL FORMAT

The general formats of the redirection symbols follow.

<name The standard input is read from file name instead of the terminal keyboard. For example,
sort < unsorted.data
causes the shell to reconnect standard input from your keyboard to the file unsorted.data. When the sort command opens the standard input and begins to read, it will read from the unsorted.data file, not your keyboard. After sort completes, the shell reconnects the standard input to your keyboard.
>name The standard output is sent to the file name instead of the terminal screen. If the file name does not exist, then it is created; otherwise, it is overwritten. For instance,
sort unsorted.data > sorted.data
would sort the unsorted.data file and redirect it to the sorted.data file.
>| Forces a write to a file that exists even though you have the noclobber option set to prevent such action.
>>name The standard output is appended to the file name. If the file does not exist, then it is created. For example,
sort more.data >> unsorted.data
reads the data in the more.data file and appends it to the end of the unsorted.data file.
<<[-]name This is referred to as a here document. It allows you to enter multiple lines of input as a block of standard input. The text is read from the standard input until a line containing only name is read. If any character in the first name is quoted, then no variable, command, or tilde substitutions are performed inside the here document input. If a - (hyphen) is present, all leading tabs are removed from the input text. The exclamation mark (!) is often used as name. For example,
                   tr '[a-z]' '[A-Z]' <<!
                   this text will be changed to uppercase
                   by the tr command.
                   The next line ends the input!
                   !
                   THIS TEXT WILL BE CHANGED TO UPPERCASE
                   BY THE TR COMMAND.
                   THE NEXT LINE ENDS THE INPUT!
<&fd Read the standard input from the file associated with file descriptor fd. Same as the 0<&fd notation.
>&fd Write the standard output to the file associated with file descriptor fd. Same as the 1>&fd notation. For example,
echo Error occurred after command on line $LINENO >&2
might be used in a shell script to write the standard output to the standard error.
nfd>&fd Create a new file descriptor nfd which is a duplicate of file descriptor fd. For example,
batchjob > batch.out 2>&1
sends the standard output of the batchjob program to batch.out and duplicates file descriptor 2 (the standard error) to be the same file as associated with file descriptor 1 (the standard output). The result is the standard output and standard error are written to the batch.out file.
<&- Close the standard input. Same as the 0<&- notation. For example,
exec <&-
closes the standard input. Not an intelligent move from your keyboard.
>&- Close the standard output. Same as the 1>&- notation. For instance,
2>&-
closes the standard error. Performs the same function as using 2> /dev/null. The standard error is not displayed. Again not always intelligent from your keyboard, but often useful in shell script programming.
<&p The standard input is connected to the background pipe. The pipe is established by a co-process when you end a command with &|.
exec < name Dynamically redirect input. Redirect standard input from the file name. The standard input for all subsequent commands is read from file name.
exec > name Dynamically redirect output. Redirect standard output to write to file name. The standard output of all subsequent commands will be redirected to file name.


Previous Table of Contents Next