Bash – How to Run the Same Command Multiple Times in Background

background-processbashcommand line

How is it possible to run multiple commands and background them using bash?

For example:

$ for i in {1..10}; do wait file$i &; done

where wait is a custom binary.

Right now I get an error:

syntax error near unexpected token `;'

when running the above command.

Once backgrounded the commands should run in parallel.

Best Answer

The &, just like ; is a list terminator operator. They have the same syntax and can be used interchangeably (depending on what you want to do). This means that you don't want, or need, command1 &; command2, all you need is command1 & command2.

So, in your example, you could just write:

for i in {1..10}; do wait file$i & done

and each wait command will be launched in the background and the loop will immediately move on to the next.

Related Question