RaKo RaKo - 1 year ago 91
Linux Question

Using pipes in command line arguments

I am working on an assignment on pipes in Linux. I have a program that prints

hello world
. It takes no argument.But when I run the program and provide command line arguments as
./a ls | wc
. I get the output
0 2 11
i.e. the output of
hello world
is passed through pipe to
. Can someone please explain this behaviour.

Answer Source

Because your program takes no arguments, the fact that you passed it ls as an argument makes no difference to its output: It's ignoring that argument and still emitting hello world, just as it would if ls had not been passed on its argument list.

Understand that a pipeline sets up a FIFO -- that is, it connects two programs.

./a ls ## this program's output...
wc     ## ...is connected to this program's input.

The pipeline is thus not an argument to a; it's an instruction to the shell about how to route the output of a, in this case an instruction to route it into the stdin of wc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download