Input/output redirection in the Bash shell
Data Flow
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 |
---|---|---|---|
|
stdout |
Command > File |
Writes the standard output from |
|
stdout |
Command >> File |
Appends the standard output from |
|
stderr |
Command 2> File |
Redirects the standard error output from |
|
stderr |
Command 2>> File |
Attaches the standard error output from |
|
stdout, stderr |
Command > File 2>&1 |
Writes the standard output and standard error output from |
|
stdout, stderr |
Command > File 2> File2 |
The standard output from Command ends up in |
|
stdin |
Command < File |
Redirects |
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
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.