How do I use parallel programming/multi threading in my bash script? How do I use parallel programming/multi threading in my bash script? multithreading multithreading

How do I use parallel programming/multi threading in my bash script?


The simplest way is to execute the commands in the background, by adding & to the end of the command:

#!/bin/bash#script to loop through directories to merge fastq filessourcedir=/path/to/sourcedestdir=/path/to/destfor f in $sourcedir/*do    fbase=$(basename "$f")    echo "Inside $fbase"    zcat $f/*R1*.fastq.gz | gzip > $destdir/"$fbase"_R1.fastq.gz &    zcat $f/*R2*.fastq.gz | gzip > $destdir/"$fbase"_R2.fastq.gz &done

From the bash manual:

If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background. The shell does not wait for the command to finish, and the return status is 0 (true). When job control is not active (see Job Control), the standard input for asynchronous commands, in the absence of any explicit redirections, is redirected from /dev/null.


I am not sure but you can try using & at the end of the command like this

zcat $f/*R1*.fastq.gz | gzip > $destdir/"$fbase"_R1.fastq.gz &zcat $f/*R2*.fastq.gz | gzip > $destdir/"$fbase"_R2.fastq.gz &