You are not logged in.

#1 2008-09-20 16:19:00

foutrelis
Developer
From: Athens, Greece
Registered: 2008-07-28
Posts: 705
Website

[Solved] Start a background process from within a loop?

What I'm trying to do is this:

for i in $(seq 3); do
    sleep $i &
done

but bash doesn't like the one-line version:

[foutrelis@foutboxd ~]$ for i in $(seq 3); do sleep $i &; done
-bash: syntax error near unexpected token `;'

Halp? sad

Last edited by foutrelis (2008-09-20 17:23:23)

Offline

#2 2008-09-20 16:30:06

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: [Solved] Start a background process from within a loop?

This does work in zsh:

for i in $(seq 3); do sleep $i &; done

It exits with the same error message as you got in bash though..

Offline

#3 2008-09-20 17:16:37

jcasper
Member
Registered: 2007-08-01
Posts: 14

Re: [Solved] Start a background process from within a loop?

The '&' after the sleep command is the termination of the command, so you don't need the extra ';'

$ for i in $(seq 3); do sleep $i & done

See the section "Lists" under "SHELL GRAMMAR" near the top of the bash man page.

Last edited by jcasper (2008-09-20 17:19:13)

Offline

#4 2008-09-20 17:23:05

foutrelis
Developer
From: Athens, Greece
Registered: 2008-07-28
Posts: 705
Website

Re: [Solved] Start a background process from within a loop?

@jcasper: Awesome. Thanks a bunch! smile

Offline

Board footer

Powered by FluxBB