Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
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.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

571 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