You can use GNU parallel
for this (sudo apt install parallel
):
parallel -m command <file # or
parallel -m command :::: file
parallel
takes only newline as the delimiting character and (of course) doesn’t perform any globbing, so characters like space, asterisk*
, question mark ?
and the like in the lines are no problem at all. -m
is for m
ultiple arguments like xargs
does it. If you don’t want command lines to be run in parallel, which is what parallel
does by default, add -j1
for 1
j
ob at a time – for package installation this may be advisable.
Example run
$ ls
a_test file
$ cat file
a*
b with spaces
$ parallel -m 'printf "line: %s\n"' <file
line: a*
line: b with spaces
$ parallel 'printf "line {#}: %s\n" {}' <file
line 1: a*
line 2: b with spaces
The directory contains two files: a_test
and file
. file
’s content consists of two lines: a*
and b with spaces
. The third command line in this example executes printf "line: %s\n"
with all the lines from file
as its arguments, this prints the argument with “line: ” before and a linebreak after it. As there is no globbing, the first argument a*
can’t match the existing file a_test
, but is printed as-is. The same goes for the next argument with spaces in it, the spaces are preserved instead of the argument being split into three.
The fourth command is just an addendum showing one of the many nice features of parallel
: {#}
in a command is replaced by the sequence number of the job, and as this one calls printf
with only one argument (no -m
) it is equal to the current line number. {}
is the placeholder for the argument(s) and can be omitted if there’s no other placeholder used in the command and the argument(s) should be added to the end of the command, which I did before.
Further reading on GNU parallel
: