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
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.