OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Pipelines

The ability of commands to read data from standard input and send to standard output is utilized by a shell feature called pipelines. Using the pipe operator “|” (vertical bar), the standard output of one command can be piped into the standard input of another:



command1 | command2

command1 | command2


To fully demonstrate this, we are going to need some commands. Remember how we said there was one we already knew that accepts standard input? It's less. We can use less to display, page-by-page, the output of any command that sends its results to standard output:



[me@linuxbox ~]$ ls -l /usr/bin | less

[me@linuxbox ~]$ ls -l /usr/bin | less


image

This is extremely handy! Using this technique, we can conveniently examine the output of any command that produces standard output.


The Difference Between > and |

At first glance, it may be hard to understand the redirection performed by the pipeline operator | versus the redirection operator >. Simply put, the redirection operator connects a command with a file while the pipeline operator connects the output of one command with the input of a second command.

command1 > file1 command1 | command2

A lot of people will try the following when they are learning about pipelines, “just to see what happens.”

command1 > command2

Answer: Sometimes something really bad.



image

Here is an actual example submitted by a reader who was administering a Linux- based server appliance. As the superuser, he did this:

# cd /usr/bin

# ls > less

The first command put him in the directory where most programs are stored and the second command told the shell to overwrite the file less with the output of the ls command. Since the /usr/bin directory already contained a file named “less” (the less program), the second command overwrote the less program file with the text from ls thus destroying the less program on his system.

The lesson here is that the redirection operator silently creates or overwrites files, so you need to treat it with a lot of respect.


 

Top OS Cloud Computing at OnWorks: