0

I have two shell scripts:

First : 1.sh, it reads as follows:

export PROP="my val"

Second : 2.sh, it reads as follows:

./1.sh
echo $PROP

Both have execute permission. When I run 2.sh, I am expecting that environment variable PROP set and exported by 1.sh, is visible to echo $PROP statement in 2.sh and would get printed. But output is blank indicating that PROP is not visible to 2.sh. What is wrong here?

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
Mayur
  • 75
  • 1
  • 10

1 Answers1

3

Try sourcing the script in the current process:

. 1.sh
echo $PROP

And you can then drop export altogether:

PROP="my val"

The problem is that you're running 1.sh in a new shell process, so any changes it makes to its environment are lost when the process ends. Specifically, export makes the variable available to children of the current process, so in this case it cannot affect 2.sh (the parent process).

Will Vousden
  • 32,488
  • 9
  • 84
  • 95