94

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Xander
  • 1,652
  • 2
  • 15
  • 20
  • and how are you executing these shell scripts? – linuxeasy May 28 '12 at 08:52
  • 1
    see also: http://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-scrip#176788 and http://unix.stackexchange.com/questions/3507/difference-between-environment-variables-and-exported-environment-variables-in-b – Lesmana May 28 '12 at 09:12
  • I first run 1.sh in the terminal , then run the 2.sh in same terminal... – Xander May 28 '12 at 09:13

2 Answers2

210

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."

linuxeasy
  • 6,269
  • 7
  • 33
  • 40
  • The OP is using `/bin/sh`, which on many platforms is a minimal POSIX shell and will not support the `source` command. – Fred Foo May 28 '12 at 08:55
  • 1
    hmmm.. I have mentioned about `.` and `space` . I have mentioned `source` since that's what I see people use nowadays. – linuxeasy May 28 '12 at 08:56
  • 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 answers – Xander May 28 '12 at 08:59
  • 2
    @Xander: If source doesn't works for you, you can use `` – linuxeasy May 28 '12 at 09:02
  • 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? – Xander May 28 '12 at 09:09
  • 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. – linuxeasy May 28 '12 at 09:11
  • 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. – Fred Foo May 28 '12 at 09:35
  • 3
    It's probably worth adding a link to the [reference documentation](http://ss64.com/bash/source.html) 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." – gfullam Apr 13 '16 at 14:27
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
Fred Foo
  • 355,277
  • 75
  • 744
  • 836