Redirect data streams with pipes
This Way Please
![© Lead Image © artqu, 123RF.com © Lead Image © artqu, 123RF.com](/var/linux_magazin/storage/images/issues/2022/261/pipes-in-the-shell/po-26575-123rf-artqu_123rf-enterprise_graph_with_arrows_resized.png/807266-1-eng-US/PO-26575-123RF-artqu_123rf-Enterprise_graph_with_arrows_resized.png1_medium.png)
© Lead Image © artqu, 123RF.com
Pipes in the shell offer a surprising amount of versatility, including the ability to transfer data between computers.
Many users are only familiar with pipes as links between multiple flows, but they can do much more than that. Pipes can help you transfer data between computers. In this article, I will show you how to use pipes to redirect data streams in the shell.
Channels
Whenever a process starts under Linux, it is automatically assigned three channels. These channels have system assignments that let you address them, and each has a starting and end point. Channel 0 (STDIN
) reads data, channel 1 (STDOUT
) outputs data, and channel 2 (STDERR
) outputs any error messages. Channel 2 typically points to the same device as channel 1 (Figure 1).
![](/var/linux_magazin/storage/images/issues/2022/261/pipes-in-the-shell/figure-1/807269-1-eng-US/Figure-11_large.png)
The shell itself, a Unix process, also uses these three channels. Each of them can be addressed via a file descriptor representing the respective channel number. On Linux, the channels used here physically reside in the /proc/PID/fd
directory, where PID
is equivalent to the process ID of the process being examined.
The Bash shell most commonly used on Linux also has channel 255. To make sure job control is retained when redirecting this channel, the shell sets it to STDERR at startup time.
Redirection
A redirection reads the channels of a process from a different source or outputs them to a different target. The most common use cases involve finding a string on the error channel and redirecting error messages to the /dev/null
device.
The call in line 1 of Listing 1 attempts to display the nonexistent /dev/pseudo/
directory, which generates an error message on channel 2. The call from line 5 adds a redirect from channel 2 to /dev/null
to the command. The error message now no longer appears on the screen, but the return value of the command remains unchanged.
Listing 1
Redirecting a Channel
01 # ls -ld /dev/pseudo 02 ls: cannot access /dev/pseudo: No such file or directory 03 # echo $? 04 2 05 # ls -ld /dev/pseudo 2>/dev/null 06 # echo $? 07 2
Pipes
A pipe is a special kind of file that acts as first in, first out (FIFO) memory in interprocess communication. With FIFO, one process writes to the pipe, while another reads from it. The reading process retrieves the characters in the same order as the writing process stored them. If, say, process 1 writes the values 1 Z 2 Y 3 X 4 W 5 V
to a pipe, process 2 reads them from the pipe in the same order. Linux uses two types of pipes: anonymous and named pipes.
Anonymous pipes connect commands using the pipe symbol (|
). These pipes are called anonymous because the user doesn't get to see them at runtime under normal circumstances. Anonymous pipes reside in the /proc/PID/fd/
directory like the standard channels. Calling a command sequence temporarily generates this kind of pipe.
Named pipes can be created in the filesystem using the mkfifo
command. They remain active until deleted again using the rm
command. When you use named pipes, you have to take care of the redirection work yourself, whereas the shell does this automatically for anonymous pipes.
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.
-
SUSE Offers CentOS 7 Support with Liberty Linux Lite
SUSE's Liberty Linux support offering now includes CentOS 7, which means businesses won't be forced to migrate those servers for some time.
-
Ubuntu's App Center Finally Supports Local Installs Again
If you regularly download .deb files and would prefer a GUI method of installing, Ubuntu has your back.
-
AlmaLinux Now Supports Raspberry Pi 5
If you're looking to create with the Raspberry Pi 5 and want to use AlmaLinux as your OS, you're in luck because it's now possible.
-
Kubuntu Focus Releases New Iterations of Ir14 and Ir16 Laptops
If you're a fan of the Kubuntu Focus laptops or have been waiting for the right time to purchase one, that time might be now.
-
NixOS 24.05 Is Ready for Prime Time
The latest release of NixOS (Uakari) has arrived and offers its usual reproducible, declarative, and reliable goodness.
-
Linux Lite 7.0 Officially Released
Based on Ubuntu 24.04 and kernel 6.8, Linux Lite version 7 now offers more options than ever.
-
KaOS Linux 2024.05 Adds Bcachfs Support and More
With updates all around, KaOS Linux now includes support for the bcachefs file system.
-
TUXEDO Computers Unveils New Iteration of the Stellaris Laptop Line
The Stellaris Slim 15 is the 6th generation and includes either an AMD or Intel CPU