8

I'd like to be able to use xargs to execute multiple parameters in different parts of a command.

For example, the following:

echo {1..8} | xargs -n2 | xargs -I v1 -I v2 echo the number v1 comes before v2

I would hope that it would return

the number 1 comes before 2
the number 3 comes before 4 

... etc

Is this achievable? I suspect that my multiple use of -I is incorrect.

18

I believe that you can’t use -I that way.  But you can get the effect / behavior you want by saying:

echo {1..8} | xargs -n2 sh -c 'echo "the number $1 comes before $2"' sh

This, essentially, creates an ad hoc one-line shell script, which xargs executes via sh -c.  The two values that xargs parses out of the input are passed to this “script”.  The shell then assigns those values to $1 and $2, which you can then reference in the “script”.

  • 1
    Thank you - that's great. I've been reading the man for sh though and am struggling understanding what the second call to sh does and, by extension, why its omission yields 'half' the result. – Damien Sawyer Aug 19 '17 at 1:28
  • 2
    @DamienSawyer There is no second call to sh. The trailing sh at the end is what is put into $0. $0 is usually what holds the name of the interpreter or the script. – Kusalananda May 19 '18 at 9:53
3

In the specific case of printf, you could always do:

echo {1..8} | xargs printf 'the number %s comes before %s\n'

because printf has an intrinsic xargs-like ability to execute multiple times if it is given more arguments than it needs for a single invocation.  Though that has little advantage over

printf 'the number %s comes before %s\n' {1..8}

And for large lists, the simple xargs command could result in xargs running several instances of printf, some of which might have odd numbers of arguments. You could pass -n 1000 to xargs to guard against that, where 1000 is an even number that should be small enough so as not to reach the arg list too long limit and large enough to avoid running so many printfs.

Note that xargs would call, not your shell's builtin printf, but the external printf, with each invocation in a separate new process.

Also note that for an empty input, except on some BSDs, it would still run printf once with no argument. GNU xargs and compatible have a -r (or --no-run-if-empty) option to avoid that.

To be clear, this simple answer is specific to your printf example, and would not work in the general case where you must pass two parameters at a time to your command (as would be the case for diff, for example).  To solve the general problem with zsh, you can use:

for i j ({1..8}) echo the number $i comes before $j
  • (1) IMHO, the zsh part of the above is a real answer, and the xargs-with-no-options part is just an oddity.  In such a case, I would consider putting the real answer first.  (2) I assume you have a reason for not using quotes in your zsh command.  If so, you might want to state it, lest people read the above and start to think that quotes aren’t important.  (Or simply include them, since they don’t hurt.) – Scott Jul 23 '18 at 18:50
2

try this:

echo {1..8} |xargs -n 2 bash -c 'echo "the number $0 comes before $1"'
  • 2
    This would work in this case, but it's better to run sh -c script with sh (or whatever) in $0. Note that $0 is not included in $@ if this was to be used by the sh -c script, for example if the script was echo "the two numbers were $@" – Kusalananda May 19 '18 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.