Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diff between a pipe and a filter?

Tags:

bash

shell

I'm new to shell scripting and I want to know what the difference is between pipe and filter. They both seem to do the same things. Even the format is the same.

ls -l | grep user |cat >user

A pipe or a filter?

like image 553
AnujaPL Avatar asked Jul 04 '26 01:07

AnujaPL


2 Answers

The graph below briefly describes the relationship of pipe, filter, pipeline and redirection.

pipeline

Filter is only one ELEMENT of a pipeline, all the elements in pipeline are collected by pipe. Below is the definition of Filter in Wikipedia.

In Unix and Unix-like operating systems, a filter is a program that gets most of its data from its standard input (the main input stream) and writes its main results to its standard output (the main output stream). Unix filters are often used as elements of pipelines. The pipe operator ("|") on a command line signifies that the main output of the command to the left is passed as main input to the command on the right.

like image 115
feihu Avatar answered Jul 05 '26 15:07

feihu


In your example, the whole command is a pipeline:

 ls -l | grep user | cat > user

ls is acting as the source of data. grep is then acting as a filter - a filter performs some kind of transformation on the data. The pipe (|) passes the data from ls to grep, performing buffering etc. Finally cat (which is extraneous in your example) outputs the filtered data to something (terminal, file, variable).

like image 44
Josh Jolly Avatar answered Jul 05 '26 16:07

Josh Jolly