how to obtain a variable between subroutines

#!/usr/bin/bash
sub1 () {

for ((i=0;i<10;i++))
do
  export  a=$i;
   echo "value of a is $a";
   sleep 1
done
}
sub1 &

sub2 () {

for ((j=0;j<10;j++))
do
   echo "value of a is $a";
   sleep 1
done
}
sub2

Output:

bash-3.00$ ./var_test.sh 
value of a is 
value of a is 0
value of a is 
value of a is 1
value of a is 
value of a is 2
value of a is 
value of a is 3
value of a is 
value of a is 4
value of a is 

I want the value of $a to be reflected in sub2.
Any idea ?????

This is a scope problem - sub2 cannot see changes to a made in sub1.

What are you trying to do - not how you think it should be done?

the requirement is to communicate between two processes. How does two (forked) process exchange variable values ?
Here, sub2 needs to know the value of "a" in sub1 in runtime.

Ever heard of file descriptors ? :wink:

Use a fifo.

correct me if i'm wrong, "mkfifo" will create a file and in that we feed values and retrieve.
Is there any method to get the same working without generating files.
It will be more useful if you could give an example.
Thanks in advance...

---------- Post updated at 08:08 PM ---------- Previous update was at 08:00 PM ----------

This one meets my requirement, but dont want a file to be created.

#!/usr/bin/bash
rm -rf pipe_file
mkfifo pipe_file
sub1 () {

for ((i=0;i<10;i++))
do
   a=$i;
   echo $i > pipe_file
   echo "value of a is $a";
   sleep 1
done
}
sub1 &

sub2 () {

for ((j=0;j<10;j++))
do
  echo "value of a is `cat < pipe_file`";
   sleep 1
done
}
sub2