This seems to be the safest version.
tr '[\n]' '[\0]' < a.txt | xargs -r0 /bin/bash -c 'command1 "$@"; command2 "$@";' ''
(-0
can be removed and the tr
replaced with a redirect (or the file can be replaced with a null separated file instead). It is mainly in there since I mainly use xargs
with find
with -print0
output) (This might also be relevant on xargs
versions without the -0
extension)
It is safe, since args will pass the parameters to the shell as an array when executing it. The shell (at least bash
) would then pass them as an unaltered array to the other processes when all are obtained using ["$@"][1]
If you use ...| xargs -r0 -I{} bash -c 'f="{}"; command "$f";' ''
, the assignment will fail if the string contains double quotes. This is true for every variant using -i
or -I
. (Due to it being replaced into a string, you can always inject commands by inserting unexpected characters (like quotes, backticks or dollar signs) into the input data)
If the commands can only take one parameter at a time:
tr '[\n]' '[\0]' < a.txt | xargs -r0 -n1 /bin/bash -c 'command1 "$@"; command2 "$@";' ''
Or with somewhat less processes:
tr '[\n]' '[\0]' < a.txt | xargs -r0 /bin/bash -c 'for f in "$@"; do command1 "$f"; command2 "$f"; done;' ''
If you have GNU xargs
or another with the -P
extension and you want to run 32 processes in parallel, each with not more than 10 parameters for each command:
tr '[\n]' '[\0]' < a.txt | xargs -r0 -n10 -P32 /bin/bash -c 'command1 "$@"; command2 "$@";' ''
This should be robust against any special characters in the input. (If the input is null separated.) The tr
version will get some invalid input if some of the lines contain newlines, but that is unavoidable with a newline separated file.
The blank first parameter for bash -c
is due to this: (From the bash
man page) (Thanks @clacke)
-c If the -c option is present, then commands are read from the first non-option argument com‐
mand_string. If there are arguments after the command_string, the first argument is assigned to $0
and any remaining arguments are assigned to the positional parameters. The assignment to $0 sets
the name of the shell, which is used in warning and error messages.
while
loop that can contain multiple commands. – Eulogistic"
,*
,a two spaces b
,$(echo Do not print this)
. If these do not work as expected, there are likely other bugs in the solution, too. – Flavoring