[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

System User's Guide: Operating System and Devices


Input and Output Redirection in the C Shell

Before the C shell executes a command, it scans the command line for redirection characters. These special notations direct the shell to redirect input and output.

You can redirect the standard input and output of a command with the following syntax statements:

< File Opens the specified File (which is first variable, command, and file name expanded) as the standard input.
<<Word Reads the shell input up to the line that matches the value of the Word variable. The Word variable is not subjected to variable, file name, or command substitution. Each input line is compared to the Word variable before any substitutions are done on the line. Unless a quoting character (\, ", ' or `.) appears in the Word variable, the shell performs variable and command substitution on the intervening lines, allowing the \ character to quote the $, \, and ` characters. Commands that are substituted have all blanks, tabs, and new-line characters preserved, except for the final new-line character, which is dropped. The resultant text is placed in an anonymous temporary file, which is given to the command as standard input.

> File

 >!File

 >& File

>&! File Uses the specified File as standard output. If File does not exist, it is created. If File exists, it is truncated, and its previous contents are lost. If the noclobber shell variable is set, File must not exist or be a character special file, or an error results. This helps prevent accidental destruction of files. In this case, use the forms including an ! to suppress this check. File is expanded in the same way as < input file names. The form >& redirects both standard output and standard error to the specified File. The following example shows how to separately redirect standard output to /dev/tty and standard error to /dev/null. The parentheses are required to allow standard output and standard error to be separate.

% (find / -name vi -print > /dev/tty) >& /dev/null

> >File

> >! File

> >& File 

> >&! File Uses the specified File as standard output like >, but appends output to the end of File. If the noclobber shell variable is set, an error results if File does not exist, unless one of the forms including an ! is given. Otherwise, it is similar to >.

A command receives the environment in which the shell was invoked, as changed by the input/output parameters and the presence of the command as a pipeline. Thus, unlike some previous shells, commands that run from a shell script do not have access to the text of the commands by default. Rather, they receive the original standard input of the shell. Use the << mechanism to present inline data. This lets shell command files function as components of pipelines and also lets the shell block read its input. Note that the default standard input for a command run detached is not changed to the empty /dev/null file. Rather, the standard input remains the original standard input of the shell.

To redirect the standard error through a pipe with the standard output, use the form |& rather than just the |.

Control Flow

The shell contains commands that can be used to regulate the flow of control in command files (shell scripts) and (in limited but useful ways) from shell command-line input. These commands all operate by forcing the shell to repeat, or skip, in its input.

The foreach, switch, and while statements, and the if-then-else form of the if statement, require that the major keywords appear in a single simple command on an input line.

If the shell input is not searchable, the shell buffers input whenever a loop is being read and searches the internal buffer to do the rereading implied by the loop. To the extent that this is allowed, backward gotos succeed on inputs that you cannot search.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]