How to Parallel For in Linux Bash Shell?


The Linux Bash Shell supports inherent task parallelism. For example, you can put a & (ampersand) symbol after each command to start a task in background (asynchronously). Let’s take the following shell for example:

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
 
s=0
for ((i=1;i<=5;i++));do
{
  sleep $i
  s=$((s+i))
}&
wait
echo "s=$s"
#!/bin/bash

s=0
for ((i=1;i<=5;i++));do
{
  sleep $i
  s=$((s+i))
}&
wait
echo "s=$s"

The for indeed is a “parallel for” because of the ampersand symbol. So the block of statements inside the for will be executed in parallel and asynchronously. The wait command acts as a barrier which will wait for all previous threads in the current script to finish. It takes 5 seconds (maximum thread, sleep 5) but if you take out the ampersand symbol, then the script running time will be approximately 15 seconds and you will get the result of s=15.

1
2
3
4
5
6
root@HP-PC:~# time ./test
s=0
 
real    0m5.048s
user    0m0.000s
sys     0m0.094s
root@HP-PC:~# time ./test
s=0

real    0m5.048s
user    0m0.000s
sys     0m0.094s

Inside the parallel.for, 5 threads may write to the same variable, which yields the race condition and that requires a lock/mutex to allow atomic operations. However, the parallel for allows to write powerful and simple parallel tasking in Linux shell, which improves the efficiency.

bash-shellshock How to Parallel For in Linux Bash Shell? BASH Shell batch script linux linux shell parallel computing

bash-shellshock

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
312 words
Last Post: The PHP Page Rule Checker of CloudFlare
Next Post: How to Enable AMP with WordPress? AMP Optimisation Techniques with WordPress

The Permanent URL is: How to Parallel For in Linux Bash Shell?

Leave a Reply