Question

cd folder1
ls | xargs -t -I {} {}/MyScript.sh param1 param2 &

The above command doesn't work.

I am not sure why {} is not replaced by sub-folder. How can I fix the issue?

summary for the question: below works: ls | xargs -t -I '{}' ksh '{}'/MyScript.ksh param1 param2

Was it helpful?

Solution

After some experimentation, this doesn't work:

ls | xargs -t -I {} {}/MyScript.sh param1 param2

But this does:

ls | xargs -t -I {} sh {}/MyScript.sh param1 param2

A close reading of the man page reveals why:

xargs  [...] [command [initial-arguments]]

-I replace-str
     Replace occurrences of replace-str in the initial-arguments
     with names read from standard input.

If the {} is first, it is part of the command, not the initial-arguments, and thus is not replaced. So, you need to arrange things so that the {} is always part of the arguments, and not the command. In your case, this can easily be done by using sh {}/MyScript.sh rather than invoking MyScript directly.

(Note: I'm using bash here, where {} with nothing inside it doesn't need to be quoted. Other shells may need to quote it.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top