The outputs of the programs are generally saved in files for further use. Sometimes, temporary files are created in order to use an output of a program as an input to another program. We can avoid creating temporary files and feed the output of a program as an input to another program using bash pipe and pipelines.
The pipe denoted by the operator |
connects the standard output of a process in the left to the standard input in the right process by inter process communication mechanism. In other words, the |
(pipe) connects commands by providing the output of a command as the input to another command.
Consider the following example:
$ cat /proc/cpuinfo | less
Here, the cat
command, instead of displaying the content of the /proc/cpuinfo
file on stdout
, passes its output as an input to the less
command. The less
command takes the input from cat
and displays on the stdout
per page.
Another example using pipe is as follows:
$ ps -aux | wc -l # Showing number of currently running processes in system 254
Pipeline is a sequence of programs/commands separated by the operator ' |
' where the output of execution of each command is given as an input to the next command. Each command in a pipeline is executed in a new subshell. The syntax will be as follows:
command1 | command2 | command3 …
Examples showing pipeline are as follows:
$ ls /usr/lib64/*.so | grep libc | wc -l 13
Here, we are first getting a list of files from the /usr/lib64
directory that has the .so
extension. The output obtained is passed as an input to the next grep
command to look for the libc
string. The output is further given to the wc
command to count the number of lines.
18.118.27.119