1

We have two scripts, first.sh and second.sh. We run first.sh from second.sh using the . (source) command.

I am stuck at this script,

first.sh

 value="myvalue"
 export value

 oldvalue="othervalue"
 export value

After initializing the value and exporting, we have initialized the oldvalue and again exported value instead of oldvalue , but still the othervalue is available in the script second.sh, even though we didn't export oldvalue in first.sh.

Hauke Laging
  • 90,279
nikhil2000
  • 13
  • 4
  • 1
    What other script? What about the environment (declare -p oldvalue) from which it is run? – Hauke Laging May 30 '20 at 03:50
  • @Laging...the value of variable is being used in another shellscript by using "oldvalue", so my doubt is 'though we didn't export the oldvalue, how is it referencing it? – nikhil2000 May 30 '20 at 04:28
  • You should execute declare -p oldvalue in the shell from which the other script is run. In general: You can make environment variables available only to child processes. If you want some kind of independent inter-process communication you need a different mechanism e.g. a file which is written by one script and read by the other. – Hauke Laging May 30 '20 at 04:33
  • How are you running the other script? If you are using . or source, then that script is running in the same shell where oldvalue was set. Alternatively, it is possible that oldvalue was previously exported. Setting the value does not unexport it if done after the initial export. – camh May 30 '20 at 04:41
  • the mechanism is the first.sh file have the above initialization i mentioned, and we are running the first.sh from second.sh using "." command – nikhil2000 May 30 '20 at 05:33
  • If you're sourcing the second "script" with ., you're running the content of the second file in the same script, you're not running a separate script. – Andy Dalton May 30 '20 at 05:35
  • @Laging...here i'm trying to understand an old shell script, where the "oldvalue" is used in the second sh file where the first.sh is executed. – nikhil2000 May 30 '20 at 05:35
  • @AndyDalton...thanks – nikhil2000 May 30 '20 at 05:37
  • @HaukeLaging declare: not found, this is a shell question and a standard shell does not know about declare. – schily May 30 '20 at 09:06

2 Answers2

0

If you're sourcing the second "script" with ., you're running the content of the second file in the same script, you're not running a separate script.

Consider, for example, these two scripts where one runs the other:

$ ls
script1*    script2*

$ cat script1
#!/bin/bash

export value="myvalue"
oldvalue="othervalue"

# Here script1 is running the second script (not sourcing it)
./script2

$ cat script2
#!/bin/bash

echo "value: ${value}"
echo "oldvalue: ${oldvalue}"

Note here that script1 sets and exports value and sets but does not export oldvalue, then runs script2 as a separate process. script2, in turn, tries to print both values. Here's the output:

$ ./script1
value: myvalue
oldvalue:

As you can see, your expectation about the visibility of the two variables is correct in this case -- since oldvalue was not exported from script1, its value wasn't defined in script2.

Andy Dalton
  • 13,993
  • In the question it says "but still the othervalue is available in the another script". – Hauke Laging May 30 '20 at 14:15
  • @HaukeLaging which it cannot be, as illustrated. It's much easier to answer the question once the comments reveal the misunderstanding. – Andy Dalton May 30 '20 at 14:46
  • As you say: the comments. They do reveal that one "script" is sourced by "the other". Your answer describes a completely different situation. – Hauke Laging May 30 '20 at 14:51
  • @HaukeLaging Yes, my answer illustrates (1) how to actually run one script from another and (2) that the OP's expectation of the behavior is correct when doing so. – Andy Dalton May 30 '20 at 14:53
0

The answer is that effectively you do not have two scripts but one script which is spread over two files like an include in other programming languages.

Thus the variable is not passed via the environment between the two script parts, it is available because it is part of the same shell process.

This works only because you run

. first.sh

before you access the variable. So this would work in second.sh

. first.sh
echo "$oldvalue"

but this would not

echo "$oldvalue"
. first.sh
Hauke Laging
  • 90,279