help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] how to understand the xargs


From: Dan Douglas
Subject: Re: [Help-bash] how to understand the xargs
Date: Wed, 28 Dec 2011 00:12:52 -0600
User-agent: KMail/4.7.4 (Linux/3.1.5-pf; KDE/4.7.4; x86_64; ; )

On Tuesday, December 27, 2011 12:11:59 AM lina wrote:
> Hi,
> 
> I don't understand the xargs well,
> 
> can someone help me understand the below sentence,
> 
> ls . | xargs -i -t cp ./{} {}.bak
> 
> How does xargs work,
> 
> I checked google, :
> -i : replace the string
> -t print
> but still lack well understanding,
> 
> how those xargs organise things,
> 
> and
> $ ls .
> a.txt b.txt
> 
> above will give a.txt.bak b.txt.bak
> ls | xargs -i cp ./{} {basename {}.bak}
> definitely not work,
> 
> Thanks with best regards,

In addition to Greg's remarks about being unable to safely handle arguments, 
xarg's usefulness is severely limited by randomly invoking your command 
multiple times with some random number of arguments. If I'm not mistaken, 
there's no way of controling that, and exactly how it's done isn't very well 
documented. xargs doesn't just mean "take the input, split it into words, and 
run the program with args." It's similar to "find -exec command {} +" in that 
respect.

This is why, IMHO, any shell without arrays is completely broken and useless 
with regards to it's most important task: running commands with args. The only 
safe mechanism you're left with without them is "$@".

Always use read -a (-A in ksh and zsh), read in a loop, or mapfile. They're 
totally invaluable. I put an example over here that even "looks" a lot like 
xargs:

http://wiki.bash-hackers.org/commands/builtin/mapfile

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]