xargs tip
Posted Tue, 10 Jul 2007
Under normal circumstances, I use this kind of xargs invocation:
xargs -n1 -I@ sh -c 'wget http://@/ | sed -e "s/^/@ /"'The one argument passed to each invocation is replaced by '@'. This sucks if you have awkward characters such as quotes. Instead, use sh's argument processing.
# Failed invocation due to quotes: easel(~) % echo "one\n'\"two'\nthree" | xargs -n1 -I@ sh -c 'echo "@"' one sh: -c: line 0: unexpected EOF while looking for matching `"' sh: -c: line 1: syntax error: unexpected end of file three # Successful invocation: % echo "one\n'\"two'\nthree" | xargs -n1 sh -c 'echo "$1"' - one "two threeThe trailing - is required, because sh will set $0, $1, etc, based on those arguments. For example:
% sh -c 'echo "$0, $1"' foo bar foo, barIn an effort to use the shell "properly" I use $1 and pass - as the $0 argument. This lets you do neater things that the
-I flag doesn't,
such as multiple arguments in a given invocation.
% echo "one\ntwo\nthree\nfour" | \ xargs -n2 sh -c 'echo $1 and $2' - one and two three and fourSuper useful.