What are pipelines?

In a Unix environment, pipelines are used to transfer the output of one process as an input to another process. The | character defines them.

When we are working with multiple programs, processing data can be cumbersome if stored in temporary files. Managing these files takes extra effort. We have to keep in mind their paths and remove them after usage.

Here is where pipelines can help us share data between programs by passing messages instead of using the file system.

Example

In this example, we will see how data is transferred from one process to another using the pipe operator. We will be using the ls and head commands to see how this works.

Connect the terminal provided below and run the commands listed in this section.

We know that the ls command outputs the list of files present in the current directory. To check the files present, write ls in the terminal below.

Now we will be listing the first three files in the directory, using the pipe command:

ls | head -3

You can copy-paste this command in the terminal below to check that the first three files are printed in the directory.

The pipe command | works by sending the output of the ls process to the head command.

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved