-->
Previous Table of Contents Next


Redirecting Input and Output

Many programs expect input from the terminal or keyboard; many programs send their output to the terminal screen. Linux associates keyboard input with a file named stdin; it associates terminal output with a file named stdout. You can redirect input and output so that rather than come from or go to the terminal, it comes from a file or is sent to a file.

Use the < (less than) symbol to redirect input into a command or program so that it comes from a file instead of the terminal. Suppose that you want to send a file named info by e-mail to someone whose address is sarah. Rather than retype the contents of the file to the mail command, give this command to use the info file as the input (stdin) to the mail command:


mail sarah < info

Use the > (greater than) symbol to redirect the output of a program to a file. Instead of going to the terminal screen, the output is put into a file. The command date displays the current time and date on the terminal screen. If you want to store the current time and date in a file named now, enter this command:


date > now


CAUTION:  
If the filename on the right side of the > already exists, it will be overwritten. Be careful not to destroy useful information this way.

If you want to append, or concatenate, information to an existing file, use the two-character >> symbol. To append the current date to a file named report, enter the following command:


date >> report

For a slightly more lengthy example, suppose that the file named sales consists of sales data; the first field of each line contains a customer ID code. The first command line puts the output of the date command into a file named sales_report. The second command line uses the sales file as input to the sort command and appends the output to the sales_report file. The last line sends the sales_report file to users sarah and brad by e-mail:


date  >  sales_report

sort  <  sales  >>  sales_report

mail  sarah  brad  <  sales_report


CAUTION:  
Be careful not to redirect the same file as both input and output to a command. Most likely, you’ll destroy the contents of the file.

Table 18.5 summarizes the redirection symbols used in Linux.

Table 18.5 Linux’s Redirection Symbols

Symbol Meaning Example

< Take input from a file mail sarah < report
> Send output to a file date > now
>> Append to a file date >> report

Substituting Shell Variables

You learned about shell variable expansion earlier in this chapter when you set your PATH variable to PATH=$PATH:newpath. The shell replaced $PATH with the current values of the PATH variable. Shells are really interpreted languages, almost like BASIC; the shell variable is the primary object manipulated. Because shell variables are frequently manipulated, each shell provides methods of testing and defining the shell variables.

Shell variables are stored as strings. When two variables are placed together, their respective strings are concatenated. For example, if you have two variables, X=hello and Y=world, the expression $X$Y results in the string helloworld. If you give the following command, the shell parses the two parameters and the values of X and Y (the two strings hello and world) are substituted before being passed to the echo command:


echo $X $Y

The echo command then prints hello world.


NOTE:  If you place a dozen tab characters between $X and $Y, the output results are still the same.

If the substitution can be ambiguous, the shell picks the most obvious substitution—often with unpredictable results. For example, if you type echo $XY, the shell substitutes helloY. If you also had a variable XY, its value is substituted instead. To get around these ambiguities, the shell has a simple mechanism to allow you to define exactly what you mean. If you type ${X}Y, the shell substitutes the value of X before appending the character Y to the string.

The Bourne and Korn shells have a rich collection of shell-variable expansion techniques that perform various tests on the variable before making the substitution. See the man pages for sh and ksh for more details.

Substituting Command Results

After the shell performs its substitution of variables, it scans the line again for commands to be run before the command line is finally ready. Command substitution means that Linux substitutes the results of a command for a positional parameter. This is specified in the following way:


command-1  parameter  ‘command-2’


Previous Table of Contents Next