Notice! This document is currently in Archived status.
The content of this document may be incorrect or outdated.

Print this article Edit this article

How do I redirect standard input and output?

Purdue Engineering Computer Network
Most UNIX system commands take input from your terminal and send the resulting output back to your terminal. A command normally reads its input from a place called standard input, which happens to be your terminal by default. Similarly, a command normally writes its output to standard output, which is also your terminal by default.

This action can be changed by redirecting standard input and standard output from and to any other file.

Redirecting standard input

To redirect the standard input for a command use the < (less than) symbol followed by the name of the input file. For example:

	mail cathy < letter
This redirects the standard input to the mail command so that it comes from the file letter. The effect of this is to mail the contents of this file to the user Cathy.

Redirecting standard output

To redirect the standard output from a command use the > (greater than) symbol followed by the name of the output file. If the file that you redirect standard output to does not already exist, it will be created. For example:

	grep jruser /etc/passwd > staff
This redirects the standard output from the grep command so that it goes to the file staff. This file will contain all occurrences of the string jruser that were found in the /etc/passwd file.

NOTE: Redirecting standard output to a file that already exists overwrites its contents with the standard output. You can use set noclobberto prevent yourself from doing this accidentally.

Appending standard output to a file

To append the standard output from a command to a file use two >> (greater than) symbols followed by the name of the file. If the file does not exist, it is created. For example:

	cat part1 part2 part3 part4 >> chapter2
This reads files part1 through part4 in turn and appends them to the file chapter2. The contents of part2 are appended to part1 and so on.

Last Modified: Dec 19, 2016 11:12 am US/Eastern
Created: May 31, 2007 1:13 pm GMT-4 by admin
JumpURL: https://eng.purdue.edu/jump/8a937