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
-
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.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.