Suppose we have a txt file file.txt with each line is a python script e.g. python a1.py python a2.py python a3.py

What does the following command is really doing ?

cat file.txt | xargs -n 1 -P $NUM_CPUS bash -c

  • this forum is not the best place to ask if this is a homework....first step, read man xargs....then read man bash for the -c flag... – mazs Oct 20 '17 at 13:50


cat file.txt | xargs -n 1 -P $NUM_CPUS bash -c
  • cat file.txt - concatenate the file file.txt contents passed to next program in pipeline

  • xargs -n 1 - accept 1 argument from the STDIN(previous command output) at a time

  • -P $NUM_CPUS - run up to $NUM_CPUS processes at a time (one input line for each process invocation)

  • bash -c - the command executed with current argument


Assuming $NUM_CPUS contains an integer, then the command will start at most that many parallel non-interactive bash shells (that's what -P does). Each shell will run one line from the input file (due to -n 1).

The cat command is not needed:

xargs -n 1 -P "$NUM_CPUS" bash -c <file.txt

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.