xargs guide
Daniel Shahaf
d.s at daniel.shahaf.name
Thu Jul 30 18:37:35 IDT 2015
Steve Litt wrote on Wed, Jul 29, 2015 at 21:37:47 -0400:
> Hi all,
>
> xargs is a gender-changer type program that adapts one program's stdout
> to the next program's command line arguments. It's extremely handy for
> shell scripting, but it can be tricky. I've written a short guide for
> xargs that shows how to get around the usual xargs landmines:
>
> http://www.troubleshooters.com/linux/xargs.htm
>
> Hope you like it.
I looked for a warning that xargs parses quotes by default and didn't
find it. For example, if the input is «"foo bar"» (9 bytes), then xargs
will treat that as the single argument «foo bar», not as two arguments
«"foo» and «bar"».
There's no discussion of which switches are specific to GNU xargs and
which are available on other systems. For example, FreeBSD has -P but
not --max-procs, while POSIX has neither.
In the second example, plain "ls | cat" would have the same output, so
it's not clear what effect the RHS of the pipe has. You should probably
mention explicitly that xargs' input in the 'ls | xargs --max-lines=1'
case is not what 'ls' prints when its output is a tty (or pick
a different command for the LHS of the pipe).
None of the examples pass "--" to the xargs subcommand (as in "xargs ls
-d --") to protect against filenames with initial minuses.
The filter example uses \. and $ within a double-quoted string. The \.
is not forward-compatible (a future version of either sh(1) or regex(3)
might define it as an escape sequence). The $ inside "" might mislead
readers who don't know shell quoting rules to think $ is not
a metacharacter inside "". I'd change to single quotes, which would
address both issues.
So overall, I'd say this is a nice introduction but it glosses over some
subtleties.
HTH
Cheers,
Daniel
More information about the Linux-il
mailing list