Re: export variables to outside of a script (472 Views)
Reply
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 1 of 16 (472 Views)
Accepted Solution

export variables to outside of a script

I have a script that defines fruit and I want other scripts to see it.

#!/bin/ksh
export fruit=orange

but, after running the script, other scripts still can not see it :-(

how do I make it happen?

thank you
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 16 (472 Views)

Re: export variables to outside of a script

Hi Gemini:

Source (read) the script with the variable(s) you need. For example:

# cat /tmp/A
#!/usr/bin/sh
fruit=apple

# cat /tmp/B
#!/usr/bin/sh
fruit=apple

# /tmp/B
I want a(n) apple

...Note the dot, followed by a space, followed by the script to be sourced (read).

Regards!

...JRF...
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 3 of 16 (472 Views)

Re: export variables to outside of a script

I am confused..

you defined apple both in /tmp/A and /tmp/B......

am I missing something..
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 16 (472 Views)

Re: export variables to outside of a script

Hi (again):

No, you're not missing something, I am! :-))

# cat /tmp/A
#!/usr/bin/sh
fruit=apple

# cat /tmp/B
#!/usr/bin/sh
. /tmp/A
echo "I want a(n) ${fruit}"

# /tmp/B
I want a(n) apple

Regards!

...JRF...
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 5 of 16 (472 Views)

Re: export variables to outside of a script

ah!

so easy, but I made it too complicated!!

thank you!!!

Honored Contributor
Kenan Erdey
Posts: 559
Registered: ‎02-13-2006
Message 6 of 16 (472 Views)

Re: export variables to outside of a script

hi;

when you export a variable it's only be used by current and it's child shells. if you export it within a script, it doesn't pass to parent shell. so you can't get the value.
Computers have lots of memory but no imagination
Acclaimed Contributor
Dennis Handly
Posts: 25,094
Registered: ‎03-06-2006
Message 7 of 16 (472 Views)

Re: export variables to outside of a script

Kenan Erdey said:
>when you export a variable it's only be used by current and its child shells.

Right. The only way to see it is to either source it as JRF said, or to return a string with the info. Similar to:
eval $(resize)
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 8 of 16 (472 Views)

Re: export variables to outside of a script

you are right.....now, other scripts can not use it..

what do you meant by "eval $(resize)"
Acclaimed Contributor
Dennis Handly
Posts: 25,094
Registered: ‎03-06-2006
Message 9 of 16 (472 Views)

Re: export variables to outside of a script

>what do you meant by "eval $(resize)"

Basically resize (for X windows) wants to change the LINES and COLUMNS variables. Since it can't export those values (it's an executable), it outputs to stdout:
$ resize
COLUMNS=80;
LINES=36;
export COLUMNS LINES;

And then eval does the export, kind of like sourcing it, in the current shell.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 10 of 16 (472 Views)

Re: export variables to outside of a script

so, I have to export the variables again...hmmmm...

go back to square 1....
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 11 of 16 (472 Views)

Re: export variables to outside of a script

then, how do I export variables out of my scripts :-(


Acclaimed Contributor
Dennis Handly
Posts: 25,094
Registered: ‎03-06-2006
Message 12 of 16 (472 Views)

Re: export variables to outside of a script

>then, how do I export variables out of my scripts :-(

Assuming your script doesn't have any other output, you would use:

echo "export FOO=$FOO;"
echo "export BAR=$BAR;"

Then the callers of your script would use:
eval $(sub_script_that_wants_to_export)
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 16 (472 Views)

Re: export variables to outside of a script

Hi (again) Gemini:

Look at the manpages for 'resize'. Dennis is pointing out that another way we can communicate between processes is to have a process return information by echoing (printing) a string:

# cat /tmp/C
#!/usr/bin/sh
echo $(resize)

# cat /tmp/D
#!/usr/bin/sh
VAR=$(/tmp/C)
echo "I was told ${VAR}"

# /tmp/D
I was told COLUMNS=80; LINES=24; export COLUMNS LINES;

...Now, using our FRUIT variable:

# cat /tmp/E
#!/usr/bin/sh
echo "export FRUIT=oranges"

# cat /tmp/F
#!/usr/bin/sh
eval $(/tmp/E)
echo "I was offered some ${FRUIT}"

# /tmp/F
I was offered some oranges

The 'eval' essentially makes a two-pass sweep to allow parameter substitution for keywords and characters that would otherwise be unrecognized in the resulting commands.

Regards!

...JRF...
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 14 of 16 (472 Views)

Re: export variables to outside of a script

thanks, the fruit example was clear.

I got it now!!

thank you very much!

you guys are really good!!

Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 15 of 16 (472 Views)

Re: export variables to outside of a script

It also helps to understand subshells. When you simply run a script with it's name, a new shell is started (a subshell) and your script is interpreted (run) in that shell. When the subshell reaches the end of the script, it terminates and all the changes disappear with the subshell.

The term 'sourcing' is somewhat unique to Unix and describes the smallest shell command, namely the . (dot). What the dot accomplishes is to prevent a subshell from running, but instead, interprets the script in the current shell. So you 'source' your script and all the changes inside the script stay in the current shell. If your script exports any variables, they are available to other scripts (and programs). The env command shows all the variables which will pass to child processes (aka, the environment).
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 16 of 16 (472 Views)

Re: export variables to outside of a script

Hi,

for this purpose I once made a script 'add2env'. In scripts, which need such a feature I call it in a way like that:
...
eval `add2env -B -d: LD_LIBRARY_PATH /path1 /path2`

add2env is written in tcsh (the project requesting the programmed features wanted it that way ...) using builtin commands only - no external progs are used.
add2env takes care not to enter values in array-like variables twice.

Containing some german words, the usage is understandable nevertheless, I think.

usage: add2env [-d delimchar] ADD2THISVAR val1 ...
Optionen:
-B Bourne/KornShell Stil
-C Tcsh/CSH Stil (default)

-D Delete der Werte statt add
-l lokale Variablen (statt Environment)
-d c delimiter ist 'c' (default: ' ')
nur bei ADD:
-0 neue Werte vorne hinsetzen
-1 neue Werte an vorletzte Stelle hinsetzen
nur bei DELETE:
-u unset der Variable, wenn leer

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.