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

vikeng21
  • 543
  • 8
  • 28

1 Answers1

5

use source:

source ./set-vars1.sh

Or:

. ./set-vars1.sh
#The first . is intentional
Jahid
  • 21,542
  • 10
  • 90
  • 108
  • i tried both the options but still getting blank values thanks – vikeng21 May 14 '15 at 15:27
  • @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"` – Jahid May 14 '15 at 15:42
  • 1
    thanks a lot. It worked after removing the Exit Status. – vikeng21 May 15 '15 at 10:43