3

I am a newbie in Shell Scripting and i am trying to export a variable from child script to a parent script.below is the code and unfortunately its not working. not sure what is the issue. below is the code.

I have a script set-vars1.sh code is as below

#!/bin/bash
line_write="R"
FLAG=''
if [[ $line_write > " " && $line_write == "R" ]]
 then
  echo "Exporting"
  export FLAG=Y
  #export $FLAG;
elif [[  $line_write > " " && $line_write== "N" ]]
  then
  export FLAG=N
fi

EXIT_STATUS=-1
exit $EXIT_STATUS

I am trying to call this from set-vars2.sh the code is as below

#!/bin/bash
./set-vars1.sh
 echo $FLAG

When i try to run set-vars2.sh i am not able to echo the value of FLAG and its always blank.

Can you please let me know what is wrong here and how i can correct the same. been breaking my head a long time on this. so any help would be hugely helpful

1
  • 1
    do a source ./set-vars1.sh or . ./set-vars1.sh to make them available for the shell ! Commented May 14, 2015 at 15:23

1 Answer 1

5

use source:

source ./set-vars1.sh

Or:

. ./set-vars1.sh
#The first . is intentional
Sign up to request clarification or add additional context in comments.

3 Comments

i tried both the options but still getting blank values thanks
@vikeng21 That's because you are exiting before echo gets to print the FLAG. remove the exit command from set-vars1.sh And you are missing a space here: $line_write== "N"
thanks a lot. It worked after removing the Exit Status.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.