Gnu Parallel: While Loop with Sequential Commands

I need to run a bash script on a list of subjects. I would like to build a while loop, that runs on multiple subjects at the same time. However, the commands within the loop must run sequentially.

May you please confirm me that a sintax like this would do the job?

file="subjects.txt"

foo () {

command $subj etc
command 2 $subj etc
command 3 $subj etc
}

while read subj; do foo "$subj" &
done <$file

Thank you in advance for your help.

Ramtin

1 Answer

file="subjects.txt"

foo () {
  subj="$1"
  command $subj etc
  command 2 $subj etc
  command 3 $subj etc
}
export -f foo

parallel foo :::: "$file"

It will run n jobs in parallel (where n = number of CPU threads). To change that use parallel -j10 for 10 jobs in parallel.

Read chapter 1+2 of (can be downloaded at ). Your command line will love you for it.

5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest