Find the answer to your Linux question:
Results 1 to 3 of 3
Hi , One of my file t1.sh has got some variables with values set. When I run the second script I can not get the values of variables set in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    16

    values of variables lost between two shell scripts


    Hi ,
    One of my file t1.sh has got some variables with values set.
    When I run the second script I can not get the values of variables set in 1st script. Though I can only echo SYS Level set variables.

    Any idea?

    Many thanks

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,392
    Completely normal.
    Variables are only valid within one process.
    One method is to call the second script with parameters, and then parse the parameters into variables within the second script.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Jun 2011
    Location
    Belgium
    Posts
    84
    This is possible, but you must export the variables and execute t1.sh in the same process as your script:
    Code:
    $> cat t1.sh
    #!/bin/bash
    export FOO="bar"
    BAR="foo"
    
    $> cat script.sh
    #!/bin/bash
    echo "var FOO equals: <$FOO>"
    echo "var BAR equals: <$BAR>"       
    
    $> . ./t1.sh
    $>
    $> ./script.sh
    var FOO equals: <bar>
    var BAR equals:<>
    
    $>. ./script.sh
    var FOO equals: <bar>                                                  
    var BAR equals: <foo>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •