Operating System - Linux
1828352 Members
3134 Online
109976 Solutions
New Discussion

Re: export variables to outside of a script

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

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
15 REPLIES 15
James R. Ferguson
Acclaimed Contributor

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...
Gemini_2
Regular Advisor

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..
James R. Ferguson
Acclaimed Contributor
Solution

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...
Gemini_2
Regular Advisor

Re: export variables to outside of a script

ah!

so easy, but I made it too complicated!!

thank you!!!

Kenan Erdey
Honored Contributor

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
Dennis Handly
Acclaimed Contributor

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)
Gemini_2
Regular Advisor

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)"
Dennis Handly
Acclaimed Contributor

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.
Gemini_2
Regular Advisor

Re: export variables to outside of a script

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

go back to square 1....
Gemini_2
Regular Advisor

Re: export variables to outside of a script

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


Dennis Handly
Acclaimed Contributor

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)
James R. Ferguson
Acclaimed Contributor

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...
Gemini_2
Regular Advisor

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!!

Bill Hassell
Honored Contributor

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


Bill Hassell, sysadmin
Peter Nikitka
Honored Contributor

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"