Parallel commands in Bash

Bash natively supports a wait command.

Run parallel processes

Using default wait behavior

By default, wait will wait for all child processes to complete before continuing an execution. For most cases, this is sufficient.

1
2
3
4
5
sleep 2 && echo "hi" &
sleep 2 && echo "hi2" &

# Wait for child processes to finish
wait

Using the & character at the ending of a line sends the process to the background to complete. Without wait, the the script would end.

The script will complete in 2 seconds.

Output variation 1:

1
2
hi
hi2

Output variation 2:

1
2
hi2
hi

Using wait with process ids

wait allows for a process id argument value. This may be useful for establishing chained waits.

In the example below, the “hi3” task depends on “hi”; however “hi2” may run in parallel with “hi”.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
sleep 2 && echo "hi" &
hi_pid=$!

# "hi2" does not require "hi"
sleep 2 && echo "hi2" &

wait $hi_pid

# "hi3" requires "hi"
sleep 2 && echo "hi3" &

wait

Output:

1
2
3
hi2
hi
hi3

The process id of the last execution is returned using $!.