Redirecting Input and Output - Shell Scripting

There are times when you’d like to save the output from a command instead of just having it displayed on the monitor. The bash shell provides a few different operators that allow you to redirect the output of a command to an alternative location (such as a file). Redirection can be used for input as well as output, redirecting a file to a command for input. This section describes what you need to use redirection in your shell scripts.

Output redirection

The most basic type of redirection is sending output from a command to a file. The bash shell uses the greater-than symbol for this:

command outputfile

Anything that would appear on the monitor from the command instead is stored in the output
file specified:

$ date > test6
$ ls -l test6
-rw-r--r-- 1 rich rich 29 Sep 24 17:56 test6
$ cat test6
Tue Sep 24 17:56:58 EDT 2007
$

The redirect operator created the file test6 (using the default umask settings) and redirected the output from the date command to the test6 file. If the output file already exists, the redirect operator overwrites the existing file with the new file data:

$ who > test6
$ cat test6
rich pts/0 Sep 24 17:55
$

Now the contents of the test6 file contain the output from the who command.

Sometimes, instead of overwriting the file’s contents, you may need to append output from a command to an existing file, for example if you’re creating a log file to document an action on the system. In this situation, you can use the double greater-than symbol (>>) to append data:
$ date test6
$ cat test6
rich pts/0 Sep 24 17:55
Tue Sep 24 18:02:14 EDT 2007
$

The test6 file still contains the original data from the who command processed earlier, plus now it contains the new output from the date command.

Input redirection

Input redirection is the opposite of output redirection. Instead of taking the output of a command and redirecting it to a file, input redirection takes the content of a file and redirects it to a command.

The input redirection symbol is the less-than symbol (<):

command inputfile

The easy way to remember this is that the command is always listed first in the command line, and the redirection symbol ‘‘points’’ to the way the data is flowing. The less-than symbol indicates that the data is flowing from the input file to the command.

Here’s an example of using input redirection with the wc command:

$ wctest6
2 11 60
$

The wc command provides a count of text in the data. By default it produces three values:

  • The number of lines in the text
  • The number of words in the text
  • The number of bytes in the text

By redirecting a text file to the wc command, you can get a quick count of the lines, words, and bytes in the file. The example shows that there are 2 lines, 11 words, and 60 bytes in the test6 file.

There’s another method of input redirection, called inline input redirection. This method allows you to specify the data for input redirection on the command line instead of in a file. This may seem somewhat odd at first, but there are a few applications for this process.

The inline input redirection symbol is the double less-than symbol . Besides this symbol, you must specify a text marker that delineates the beginning and end of the data used for input. You can use any string value for the text marker, but it must be the same at the beginning of the data and the end of the data:

command marker
data
marker

When using inline input redirection on the command line, the shell will prompt for data using the secondary prompt, defined in the PS2 environment variable. Here’s how this looks when you use it:

$ wc << EOF
test string 1
test string 2
test string 3
EOF
3 9 42
$

The secondary prompt continues to prompt for more data until you enter the text marker. Thewc command performs the line, word, and byte counts of the data supplied by the inline input redirection.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Shell Scripting Topics