Input/output redirection in the Bash shell

Data Flow

© Lead Image © xyzproject, 123RF.com

© Lead Image © xyzproject, 123RF.com

Article from Issue 179/2015
Author(s):

The Bash shell lets you string several commands together if the capabilities of a single tool do not meet your needs.

Bash commands are very useful for configuring, managing, troubleshooting, and hundreds of other tasks. The power of the shell, however, really becomes apparent when you learn to chain individual programs and redirect the output.

You can link individual commands together in Bash in just a few steps. Instead of the sequence in the first three lines of Listing 1, you can combine the individual work steps and, using the semicolon, tell the interpreter to run all commands one after another (last line).

Listing 1

Linking Commands

 

You can gain even more control by imposing a condition on Bash – for example, by only letting Bash run a second (or third or nth) command if another command was successful or unsuccessful.

You can precede the rm command with the word test, for example, to delete only a certain file if it is actually available:

$ test -w <File> && rm <File>

In this case, the test program determines whether the file exists and whether it can be written to (with the -w parameter). The rm command then only deletes the file if this is the case.

If you frequently build software from source and use the three steps

$ ./configure; make; sudo make install

you can combine all three commands and, with &&, ensure that each following command only continues if the predecessor did not produce any errors:

$ ./configure && make && \
  sudo make install

In addition to the -w option, the test command has a few other practical switches to offer. Using -d, for example, you can determine whether a directory already exists and then create one only if it does not:

$ test -d <Folder> || mkdir <Folder>

In doing so, the two pipe characters between the commands take over the function of a logical OR.

Channel System

The shell uses three channels for inputting and outputting commands: programs read their data from standard input (stdin, channel 0) or from a file. A program writes its output to standard output (stdout, channel 1), and finally, error messages end up at standard error output (stderr, channel 2):

Command < Input > Output 2> Error

The < and > operators indicate direction: If standard input is not from the keyboard, < makes sure the command loads it from a file. The > operator is used to redirect a command's output to a file.

You can also access the error output via the > operator. However, you can specify the channel by prefixing the file descriptor (2>). Table 1 shows an overview of the most commonly used redirection scenarios, and in the following section, I demonstrate the use of these operators with some detailed examples.

Table 1

Shell Redirections

Channel

Stream

Command

Result

1

stdout

Command > File

Writes the standard output from Command to File

1

stdout

Command >> File

Appends the standard output from Command to File

2

stderr

Command 2> File

Redirects the standard error output from Command to File

2

stderr

Command 2>> File

Attaches the standard error output from Command to File

1 and 2

stdout, stderr

Command > File 2>&1

Writes the standard output and standard error output from Command to the same File

1 and 2

stdout, stderr

Command > File 2> File2

The standard output from Command ends up in File; the standard error output goes to File2

0

stdin

Command < File

Redirects File to the standard input from Command

Redirecting Output

As already mentioned, the > operator redirects a program's output to a file. Instead of >, you could also write 1> because it is the first channel; however, the information is not absolutely necessary because the shell always uses the standard output if it does not have any other information:

$ ls /etc > etc_content.txt

If the file behind the operator already exists, the shell simply overwrites it. The previously mentioned test tool, or doubling the operator, provides an approach to avoid trouble:

$ test -w etc_content.txt || \
  ls /etc > etc_content.txt
$ ls /etc >> etc_content.txt

The two greater-than signs in the second command basically mean that the shell appends the command output from ls to the etc_content.txt file if it already exists. If the file does not already exist, the shell creates the file and writes the output to it.

Catching Error Messages

You can redirect the stderr channel by placing the number 2 before the > operator. This is particularly useful if there are so many program error messages flying over the terminal that they disturb you when reading the other output (Listing 2).

Listing 2

Error Messages

 

You can dump error messages into a black hole /dev/null on your computer using the following command:

$ find /home -name "*.tex" 2> /dev/null

Thanks to redirection to the notorious "null device," the error messages disappear rather than spoiling the standard output.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Command Line: Data Flow

    Working in the shell has many benefits. Pipelines, redirectors, and chains of commands give users almost infinite options.

  • Bash 4.0 Introduces Associative Arrays

    The GNU Project's Bourne Again Shell (bash) is now in its fourth major version, which provides numerous enhancements.

  • Hotwire Interactive Shell

    The Hotwire shell removes the complexities of interactive consoles. We’ll show you how to Hotwire your Linux with this handy text and graphical shell for developers and administrators.

  • Automating FTP

    If you find your self executing the same few steps in FTP, you’ll save time and effort with a little automation.

  • Bash 4

    Despite the Bourne-again shell's biblical age and high level of maturity, developers continue to work on it. We take a look at the latest Bash release.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News