We are in an era which our machines have many cores. We also have languages which can use those cores in simple ways.

Imagine you want to boil eggs in parallel in a new language like Go:

var wg sync.WaitGroup func boilAnEgg(i int) {

time.Sleep(time.Second)

fmt.Println("Your egg is ready: ",i)

wg.Done()

} func main(){

wg.Add(10)

for i:=1;i<=10;i++ {

go biolAnEgg(i) # Adding "go" here simply will run the function as a goroutine here

}

wg.Wait()

fmt.Println("All eggs are ready")

}

But what about our 40 years old bash?

Lets boil our eggs in parallel in bash.

boil_an_egg() {

sleep 1

printf "Your egg is ready: $1

"

} for i in $(seq 1 10)

do

boil_an_egg $i &

done

wait

printf "All eggs are ready

"

Adding “&” at the end of your command will push it into background to run in parallel.

The command “wait” waits for all your background processes to finish.

It is that simple.

As an example when I needed to clone around 40 github repositories for some reason in travis-ci, only using this method, I was able to bring down the cloning time from 120 seconds to around 18 seconds.

This is how simple it was:

OUT=$(mktemp)

for i in garbage queue prime

do

(git clone git@github.com:kavehmz/$i.git || echo "cloning failed for: $i" >> $OUT ) &

done

wait

[ -s $OUT ] && cat $OUT && exit 255

echo "Git repositories are ready"

Bonus

List the running jobs:

sleep 10 &

sleep 15 &

jobs [2] + Running sleep 15

[1] - Running sleep 10

Next notice first one is not in the list! You can use braces to your advantage:

(sleep 10 &)

sleep 15 &

jobs [1] + Running sleep 15

But this time first job is included!:

(sleep 10) &

sleep 15 &

jobs [2] + Running sleep 15

[1] - Running sleep 10

Now lets wait only for the second job to finish (%2):

sleep 10 &

sleep 5 &

sleep 15 &

wait %2 [2] - Done sleep 5

Of course you can’t go too far with bash but as a simple tool which is ready for you in many places with no effort or installation you can use it to your advantage for many tasks.

Read more at: https://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html