Go Back    

Programming Post questions about C, C++, Java, SQL, and other programming languages here.

difference bewteen pipe, xargs, and exec


Thread Tools Search this Thread Display Modes
Old 10-12-2011
guessingo guessingo is offline
Registered User
Join Date: Sep 2009
Last Activity: 26 April 2016, 3:13 PM EDT
Posts: 57
Thanks: 1
Thanked 0 Times in 0 Posts
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?
Sponsored Links
Old 10-12-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
Join Date: Aug 2005
Last Activity: 17 October 2017, 2:51 PM EDT
Location: Saskatchewan
Posts: 22,464
Thanks: 1,139
Thanked 4,259 Times in 3,933 Posts
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.
The Following User Says Thank You to Corona688 For This Useful Post:
vinay4889 (11-17-2012)
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Display find results, and pipe to xargs mij Shell Programming and Scripting 4 06-19-2011 07:31 PM
Difference in Using xargs Karthikeyan K AIX 1 10-10-2008 08:14 PM
String substitution on find results inside exec/xargs myndcraft Shell Programming and Scripting 2 05-17-2008 02:36 PM
MV files with xargs or -exec malaymaru Shell Programming and Scripting 4 04-28-2008 10:40 AM
Difference between xargs and exec vibhor_agarwali UNIX for Dummies Questions & Answers 19 06-09-2005 07:54 AM

All times are GMT -4. The time now is 09:08 AM.