bashstyle
bashstyle copied to clipboard
When, and how, to use xargs
Since this is apparently a somewhat obscure tool for a very necessary task (converting strings to argument lists while preserving quoting/grouping), I think it's called for to have a note on using xargs -x (and xargs -xa when you can't clobber STDIN) to deserialize arguments from strings/streams, involving how to interpolate multiple inputs to output your string as a stream (use a sequence of commands in a subshell).
Also, this is one of those places where a novice Basher may be tempted to use eval. Never use eval. Not only would it be opening yourself up to arbitrary code execution, it'd do the wrong thing: if someone passes ; exit 1 to a command that's going to append its arguments to an init script, passing this to eval would cause those to be interpreted as part of the command to be evaluated. This is why you let xargs interpret the arguments.
Can you write something up on this? I'm not actually as familiar. Along with this, it could be nice to have a few useful patterns to showcase xargs.
The two best examples off the top of my head:
- How Plushu handles being started as a shell with
plushu -c <command>: https://github.com/plushu/plushu/blob/master/bin/plushush#L5 - How plushu-deploy-app-local-container integrates Docker arguments it gets from a hook: https://github.com/plushu/plushu-deploy-app-local-container/blob/master/hooks/deploy-app#L21-23
I think I wrote a few things on this in the issues for sshcommand. Basically the -x option to xargs tells it that we're constructing a single call (xargs is built to split very long streams into multiple calls by default), which should fail if it exceeds the size of a single call. (-x may also use an exec-like call to execute as a tail call, I'm not sure.) The -a option allows you to specify the arguments as a file (or a redirected process), so that STDIN can be inherited by the called function (if you use STDIN to specify the call's arguments, the launched process will get /dev/null as its STDIN).
I'm saying, can you write a little mini-guide on xarg patterns?