difference bewteen pipe, xargs, and exec

I have read several docs on these on the web and looked at examples. I can't figure out the difference. In some cases you use one or the other or you combine them.

can someone help me understand this?

First off, exec is nothing like the first two. exec does a variety of things from executing a command without returning(the shell is replaced by the new command), as well as opening/closing files.

pipes transfer the output of one program into the input of another. Without the pipe or redirection, they'd be trying to read from your keyboard and trying to print to your screen, but you can feed them data from any source and send their output anywhere you like. Shell utilities are intended to be flexible that way.

# useless example
echo asdf | cat

'echo' prints to standard output, 'cat' reads from standard input, the pipe joins them together so 'asdf' is read by cat and printed.

But what about this:

# This won't print anything
echo something | echo

echo does not read from standard input, it takes commandline parameters and nothing but parameters. So how do we translate from pipe into that?

This is what xargs is for.

echo a b c d | xargs command

is equivalent to command a b c d. Whenever you need to translate from a pipe or file into commandline arguments, xargs serves.

# This prints 'something' into xargs' input, causing xargs to run 'echo something'.
echo something | xargs echo

---------- Post updated at 10:28 AM ---------- Previous update was at 10:26 AM ----------

If you post some code which is confusing you, I'll try to explain it.

1 Like