Why doesn't a shell get variables exported by a script run in a subshell?
Asked Answered
A

2

98

I have two scripts 1.sh and 2.sh.

1.sh is as follows:

#!/bin/sh
variable="thisisit"
export variable

2.sh is as follows:

#!/bin/sh
echo $variable

According to what I read, doing like this (export) can access the variables in one shell script from another. But this is not working in my scripts.

Annadiana answered 28/5, 2012 at 8:51 Comment(3)
and how are you executing these shell scripts?Boarhound
see also: superuser.com/questions/176783/… and unix.stackexchange.com/questions/3507/…Titanium
I first run 1.sh in the terminal , then run the 2.sh in same terminal...Annadiana
B
216

If you are executing your files like sh 1.sh or ./1.sh Then you are executing it in a sub-shell.

If you want the changes to be made in your current shell, you could do:

. 1.sh
# OR
source 1.sh

Please consider going through the reference-documentation.

"When a script is run using source [or .] it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script."

Boarhound answered 28/5, 2012 at 8:54 Comment(8)
The OP is using /bin/sh, which on many platforms is a minimal POSIX shell and will not support the source command.Depressive
hmmm.. I have mentioned about . and space . I have mentioned source since that's what I see people use nowadays.Boarhound
1.sh: 3: source: not found :( I dont want my 1.sh to be executed from 2.sh, I want to run 1.sh first, after closing it run 2.sh .. and access the variable in the first from second.... Thanks for the answersAnnadiana
@Xander: If source doesn't works for you, you can use <dot><space><your program name>Boarhound
It also didnt work :( . This is very important for me.. The situation is like this.. One shellscript reads a variable, I need to use the result in that variable in several other scripts. Is there any other way for this?Annadiana
I don't know why are you getting 1.sh not found, if linux cli is handy to you, you should try tracing out as to why its not saying notfound. are you in the correct directory. And what exactly are you trying? you should try . 1.sh and not 1.sh If you try 1.sh, it will search for it in the global binary search paths.Boarhound
You're getting the "not found" error because /bin/sh will not source a file that is not in the path. Use . ./1.sh as I mention in my answer.Depressive
It's probably worth adding a link to the reference documentation and its description: "When a script is run using source [or . ] it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script."Piane
D
13

export puts a variable in the executing shell's environment so it is passed to processes executed by the script, but not to the process calling the script or any other processes. Try executing

#!/bin/sh
FOO=bar
env | grep '^FOO='

and

#!/bin/sh
FOO=bar
export FOO
env | grep '^FOO='

to see the effect of export.

To get the variable from 1.sh to 2.sh, either call 2.sh from 1.sh, or import 1.sh in 2.sh:

#!/bin/sh
. ./1.sh
echo $variable
Depressive answered 28/5, 2012 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.