how to acess a data which is set in child shell script from a parent script.

Posted on 2004-08-18
Medium Priority
Last Modified: 2010-04-21
Hi ,

I have a problem ...

I am using ksh , solaris2.8
I am executing some shell scripts in follwoing manner .

          Inside this(ABC.sh)  script i am execution
          . BCD.sh ---> in BCD.sh some variables are set and done a export of those variables.
          . XYZ.sh  --> this script executed inside ABC.sh and should acess the variables set by BCD.sh

How to do this. I exported variable in BCD.sh but I am not able to acess those in XYZ.sh .

Please give some idias.

Question by:raghava_dg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 40

Expert Comment

ID: 11836901
You can't, at least the not way you are going about it. When BCD.sh is executed it will be run by a child shell of ABC.sh. Since env variables are inherited downwards only any variable set during the execution of BCD.sh will disappear when that shell exits. What you could do would be to 'source BCD.sh', which will execute the commands in that file within the current shell. That way any variables set by BCD.sh will exist in the current shell and will thus be inheritied by the shell that executes XYZ.sh.

LVL 11

Expert Comment

ID: 11837231
As a last resort if you have no other way you can do the following

in the child script when you set the value you also write a file

as iin

echo "set ......." >  /tmp/test.tmp

and in parent script  read from that file.

Be warned that you can run into host of other issues with this

ps: this is not a good way to do it and not recommended ;)
LVL 48

Expert Comment

ID: 11838346
If you are calling BCD.sh as you showed, then there should be know problem.  Observe

$ ls
ABC.sh  BCD.sh  XYZ.sh

$ cat ABC.sh
echo start $0
. BCD.sh
echo finish $0

$ cat BCD.sh
export FOO

$ cat XYZ.sh
echo "FOO = $FOO"

$ ./ABC.sh
start ./ABC.sh
FOO = bar
finish ./ABC.sh

If you changed ABC.sh to be:

echo start $0
. BCD.sh
. XYZ.sh
echo finish $0

You wouldn't need to export FOO in BCD.sh, as it is effectively running in the same shell.

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Author Comment

ID: 11845948
yes , as said by Tintin  it should work . I tried a sample prog and ran on the same machine on same shell , it is worikng . BUt when you try with the real script which i am writing it is not working . the variable are no where resetting . going crazy !!!!.

Author Comment

ID: 11846923
now i narrowed down the problem .

i am having a while loop and inside while loop i am setting and exporting some variables . those variables are available inside while loop . But if I come out from while loop these variables are resetting to blank . In b/w these I am not resetting any PATH or variables .

my code is some thing like this.

while read tmpLine
      #Ignore any lines with hash
      line=`echo $tmpLine | egrep -v -e "#"`
      echo "Line " $line
      #Ignore the blank lines
      if [ "EMT$line" != "EMT" ]
         if [ "$ITERATE_FLAG" = "TRUE" ]
            ......do some stuff here ........


I wrote a sample prog of while loop reading a file it is working good .

any suggestions/ideas to go abt it will be highly appreciated .


Author Comment

ID: 11849126
guyz I found the solution. But I am not sure why it is behaving like this . If any 1 knows please let us know.


I used

execute < $CONFIG_FILE

while read tmpLine

I used this code insted of prev one.

Thanks for helping.
LVL 51

Expert Comment

ID: 11850327
are you shure you have not done it in a subshell somehow?
like using: ( commad whatever )

Expert Comment

ID: 11865398
The simplest solution in your case will be to write out a file  in /tmp in .kshrc style.
Source it from the parent script and then delete the file. This should set all your
variables in the file.

At the same time, if you leave the file in /tmp, it will help you debug the problem,
if your variables are not being set correctly. Visual inspection of file and repeating
the steps manually should reveal the problem.
LVL 48

Accepted Solution

Tintin earned 60 total points
ID: 11866723
From the shell FAQ.

36. Why do I lose the value of global variables that are set in a loop.

    Given the following program

      x="this is the initial value of x"
      cat dataFile | while read line;do
      echo x = $x

     You may get the following for output

       x = this is the initial value of x
     This is because in the Bourne shell redirected control structures
     run in a subshell, so the value of x only gets changed in the
     subshell, and is lost when the loop ends.

     In other shells the same result may be seen because of the way
     pipelines are handled. In shells other than ksh (not pdksh) and
     zsh elements of a pipeline are run in subshells. In ksh and zsh,
     the last element of the pipeline is run in the current shell.

     An alternative for non-Bourne shells is to use redirection
     instead of the pipeline

      x="this is the initial value of x"
      while read line;do
      done < dataFile
      echo x = $x

    With a Bourne shell you need to reassign file descriptors, so no
    pipline or redirection in the loop is involved.

      exec 3<&0         # save stdin
      exec < file
      while read line; do
      exec 0<&3        # restore stdin

    Note that putting #!/bin/sh at the top of a script doesn't
    guarantee you're using the Bourne shell. Some systems link /bin/sh
    to some other shell. Check your system documentation to find out
    what shell you're really getting in this case.

Author Comment

ID: 11877467
Thats a good explanation . I will give point to TinTin .

Author Comment

ID: 11877471
Hey TinTin one more thing .Can you please tell us where did u get this FAQ from ?
LVL 48

Expert Comment

ID: 11899277
Easiest place to get all the FAQ's is from http://www.faqs.org/

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question