File Descriptors

There are 3 standard file descriptors:

Normally input is from the keyboard or a file. Output, both stdout and stderr, normally go to the terminal, but you can redirect one or both of these to one or more files.

You can also specify additional file descriptors, designating them by a number 3 through 9, and redirect I/O through them.

File Redirection


Output redirection takes the output of a command and places it into a named file. Input redirection reads the file as input to the command. The following table summarizes the redirection options.

File Redirection
SymbolRedirection
>output redirect
>!same as above, but overrides noclobber option of csh
>>append output
>>!same as above, but overrides noclobber option on csh and creates the file if it doesn't already exist.
>& file redirect stdout and stderr to file

>>& append stdout and stderr to file

|pipe output to another command
|& command pipe stdout and stderr to command

<input redirection
<<Stringread from standard input until "String" is encountered as the only thing on the line. Also known as a "here document" (see Chapter 8).
<<\Stringsame as above, but don't allow shell substitutions

An example of output redirection is:

cat file1 file2 > file3

The above command concatenates file1 then file2 and redirects (sends) the output to file3. If file3 doesn't already exist it is created. If it does exist it will either be truncated to zero length before the new contents are inserted, or the command will be rejected, if the noclobber option of the csh is set. (See the csh in Chapter 4). The original files, file1 and file2, remain intact as separate entities.

Output is appended to a file in the form:

cat file1 >> file2

This command appends the contents of file1 to the end of what already exists in file2. (Does not overwrite file2).

Input is redirected from a file in the form:

program < file

This command takes the input for program from file.

To redirect stdout and stderr to two separate files you need to first redirect stdout in a sub-shell, as in:

% (command > out_file) >& err_file

To pipe output to another command use the form:

command | command

This command makes the output of the first command the input of the second command.


Shell commands

UNIX commands begin with a command name, often an abbreviation of the command's action (such as cp for ``copy'' or mv for ``move''). Most commands include ``flags'' and ``arguments''. A flag identifies some optional capability and begins with a hyphen. An argument is usually the name of a file, such as one to be read. For example, the command line
     cat -n stuff 
calls the cat program (to ``concatenate'' files). In this case, cat reads the file named ``stuff'' and displays it. The -n flag tells cat to number the lines in the display.

The hyphen that precedes a flag is a special character used to distinguish flags from filenames. In the example above, the hyphen prevents cat from trying to display a file named ``n''. Commands can contain other special characters as well. The shell interprets such characters, and sometimes replaces them with other values, before it passes the command with its flags and arguments to the program that actually executes the command.

Remember that uppercase and lowercase letters are not interchangeable. Thus if you tried to give the cat command by typing CAT, it would fail; there is no program named (uppercase) CAT. Or notice in the echo command shown above that SHELL (and only SHELL) must be all uppercase.

Special Characters

The following sections describe many of the characters that have special meaning to the C shell. Keep in mind that MOST punctuation characters have some special meaning. Therefore, you should not include any punctuation character other than period or underscore in the names you give to files and directories, or you may get surprising results. Also, don't try to use spaces in file and directory names.

Input/Output Redirection (the >, >>, and < Characters)

If a program normally reads its input from the terminal (referred to as ``standard input'' or ``stdin'') or writes its output to the terminal (``standard output'' or ``stdout''), you may want it to read from or write to an alternate file instead. For example, if you give the command
     who

the login names of others currently logged in are written to standard output--displayed at your terminal. If you want to have this list placed into a file called ``namelist'', you could use the character > to redirect standard output to namelist, like this:
     who > namelist 
If you already have a file named namelist, the shell erases its contents, then calls who to write the new information to namelist. If you do not have a file named namelist, the shell creates a new one before it calls the who program.

You can then do what you want with the file namelist--print it, sort its contents, display it with the cat command, or whatever.

To append output to an existing file instead of overwriting it, use the symbol >>. Thus, if you first type

     who > namelist 
and later
     who >> namelist 
The file namelist will contain two sets of results: the second set is appended to the first instead of destroying it.

Normally, you will not need to redirect standard input, as most commands that can read from standard input also accept an input filename as an argument. However, for commands that normally expect input from your terminal, the character < followed by a filename tells the command to read from that file instead. For example, one way to send mail is to type

     mail user 
and then to type the text of the message from your terminal. If the text of the message is already in a file named letter, you could redirect standard input to send the mail this way:
     mail user < letter 
Note: The shell performs input and output redirection BEFORE it calls the command you want to execute. This means that files can be accidentally destroyed. For example, the command
     sort < myfile > myfile 
destroys ``myfile'' because the shell (1) opens myfile for reading and then (2) opens it again for writing. When you open a file for writing, UNIX destroys the old contents of the file. All this happens BEFORE the shell runs the sort program to actually read the file.

Pipes (the | Character)

A pipeline is a useful way to use the output from one command as input to another without creating an intermediate file. Suppose that you want to use the who command to see who is logged in, and you want to see the results sorted alphabetically. The sort program reads a file and displays the sorted results on your terminal--the standard output. So you could accomplish what you want with the following sequence of commands:
     who > namelist
     sort namelist 
Afterward, you could give another command to get rid of the file namelist.

A pipeline enables you to do all this on a single command line, using the pipe symbol | (vertical bar). When commands are separated by the | symbol, output from the command on the symbol's left side becomes input to the one on the right of it. Thus you could type

     who | sort 
so that the results of the who program are passed to the sort program as input and displayed, by default, on your terminal.

More than one pipe symbol can be used to make a series of commands, the standard output from each becoming the standard input to the next.

Note: If a command is not capable of reading from standard input, it cannot be placed to the right of a pipe symbol.


Introduction to Unix - Ohio and Texas courses
[Contents - Ohio] [Contents - Texas]