Is it possible to initiate 10 of the same shell scripts which also initiate more copies, and have them all use the same env variable?

For example:


export COUNTER=$((COUNTER+1));
echo $COUNTER;
sleep 5;

using xargs -P 10 script.sh results in each instance creating its own scope, rather than sharing the same $COUNTER variable

I'm essentially trying to deploy script.sh 10 initial times, and then when each one completes, it automatically executes script.sh again, only this time with the newly incremented $COUNTER variable.

I've asked a similar question here, and now understand why my previous method does not work, I'm extremely interested in learning what the best way to actually accomplish this will be.

  • No, a process can't alter a variable in the parent process.
    – jordanm
    Dec 29, 2013 at 6:39
  • Yea, I've (unfortunately) been made aware of this. I'm really interested to see what the best way to accomplish a task like this will be!
    – d-_-b
    Dec 29, 2013 at 6:40
  • 1
    Use a language that supports shared variables in threads. Even then, you will have a ton of race conditions. What are you actually trying to accomplish?
    – jordanm
    Dec 29, 2013 at 6:43
  • I'm cycling through a list, and want to pass one line to a script. each time the script finishes it should start itself up again using the "next" line.
    – d-_-b
    Dec 29, 2013 at 6:44

2 Answers 2


You should take a look at GNU parallel. It can limit the number of jobs spawned in varying ways ( --jobs 10 for your application) and also has a job number ({#}) that can be passed as an argument to the script.

As Ole commented, the following should work:

find . -type f | parallel -j10 my_script {#} {}
  • 1
    find . -type f | parallel -j10 my_script {#} {}
    – Ole Tange
    Dec 29, 2013 at 12:28
  • @OleTange, thank you for comment (included in answer for better visibility). And thanks for writing parallel in first place.
    – Zelda
    Dec 29, 2013 at 13:13

IPC might be another approach. You can create a shared memory segment that will provide you with a locking mechanism where each process can then update the value stored here. This is not a file on the filesystem, it's a section of memory that is designated for this purpose.

You can read more about IPC via the man ipcs man page.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .