Search this site


Metadata

Articles

Projects

Presentations

xargs tip

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
three
The trailing - is required, because sh will set $0, $1, etc, based on those arguments. For example:
% sh -c 'echo "$0, $1"' foo bar
foo, bar
In 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 four
Super useful.

0 responses to 'xargs tip'

Showing last 0 comments... (Click here to view all comments)


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: