Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Am ashuming your not referring to: nohup allowing you to execute a command in the background – Dave Hamilton Mar 23 at 16:03
The error you're seeing is due to & and ; are both "command terminators". You don't need to use both: for ...; do wait $arg & done will work. – glenn jackman Mar 23 at 16:07
@glenn jackman. Yes, I actually tried it after posting the question. There is no need for both ; and & – Sebi Mar 23 at 16:08
For future readers, a link to the documentation: "A list is a sequence of one or more pipelines separated by one of the operators ‘;’, ‘&’, ‘&&’, or ‘||’, and optionally terminated by one of ‘;’, ‘&’, or a newline." (emphasis mine) – glenn jackman Mar 23 at 16:12
up vote 12 down vote accepted

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.

share|improve this answer

For the sake of compatibility use the posix form instead of expansion:

for i in $(seq 1 10); do (./wait file$i &); done
share|improve this answer
Note that seq is not a POSIX command and is generally only found on GNU systems. The behaviour or $(...) (and $i) depends on the current value of $IFS. Also note that by doing (cmd &), cmd will be a child of a subshell, so you won't be able to wait for and get its exit status for instance. – Stéphane Chazelas Mar 23 at 20:32

You can group the commands and put the grouped commands in background. Like :

$ for i in {1..10}; do ((wait file$i)&); done
share|improve this answer
There is no need to use the inner parentheses... – mmmint Mar 23 at 15:44
only need to use the inner parentheses if you want to put in the background multiple commands as asked in the question. Like ((sleep 1; wait file$i)&); – mazs Mar 23 at 16:17

Is your binary really named wait? I don't recommend to do so, because wait is a shell builtin.

I believe bash doesn't parse well a one-line loop that launches background processes. I suggest you to change the code to:

$ for i in {1..10}; do ./wait file$i & echo "Running 'wait' using PID=$!..."; done
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.