0

I understand the ampersand & at the end of a command sequence executes it on the background (in "parallel"), the semicolon ; allows for sequential (in "series"), and the parenthesis allows command grouping.

Suppose I have a bash file with some commands:

#!/bin/bash
cmd1
cmd2
cmd3
cmd4

If i want to apply command grouping, for example, execute the first two in paralell, wait, then execute the next two in parallel, i should put?:

#!/bin/bash
( \
cmd1 &
cmd2 &
); \
( \
cmd3 &
cmd4 & 
)

or everything must be mandatory in a single line?

#!/bin/bash
(cmd1 & cmd2 &);(cmd3 & cmd4 &)

or using the continue line character everywhere?

#!/bin/bash
( \
cmd1 & \
cmd2 &); \
( \
cmd3 & \
cmd4 & )

1 Answer 1

3

simply use & and wait

cmd1 &
cmd2 &
wait
cmd3 &
cmd4 &
wait

wait will wait for backgroup process.

as for line continuation, just use ( and ), note this is usefull only if you have redirection.

(
cmd1 &
cmd2 &
wait
) > some-log.A
(
cmd3 &
cmd4 &
wait
) > some-log.B

note that without wait, subshell will return immediatly.

date ; (
> sleep 20 &
> sleep 10 &
> ) ; date
Wed Nov 29 16:08:15 CET 2017
Wed Nov 29 16:08:15 CET 2017

compared to

date ; (
> sleep 20 &
> sleep 10 &
> wait
> ) ; date
Wed Nov 29 16:10:08 CET 2017
Wed Nov 29 16:10:28 CET 2017

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .