Operating System - Linux
1753254 Members
4318 Online
108792 Solutions
New Discussion юеВ

Re: export variables to outside of a script

 
SOLVED
Go to solution
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"