return env-variables back from a subshell

Hi!
using ksh:
I want to have a shellscript (or a subshell) returning me some env-variables set in the subshell - is this possible (I would call them "global" variables)?

thanks
witty
LVL 1
wittyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

s_franklinCommented:
That goes in the wrong direction I'm afraid. When you call a script directly, you're forking off a subprocess to interpret the script. This subprocess inherits a duplicate of your current environment variables - it can't manipulate your current ones directly. Rather it can manipulate its own copy.

What you can do is to source the script rather than execute it. i.e. if you have written "myscript.sh", if you type:

% testscript.sh

then you will launch a copy of whatever shell is required (either identified in the first line as #!/... or defaults to your current shell). This shell exists as its own process and the script will manipulate the env. vars of this subprocess.

However, you can do:

.. testscript.sh
or
source testscript.sh

If I remember correctly, the latter is how you do it in csh, but ksh acts like bash/sh and uses the ". ..." command.

This runs the script within the current shell and allows you to manipulate your current shell's environment variables.

Steve


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
s_franklinCommented:
Apologies on the previous submission - I typed one too many periods in the answer. You would type:

.. testscript.sh
or
source testscript.sh

For ksh, I believe the "." command is appropriate but if I'm wrong (haven't used it in a while), there aren't many alternatives :).

Steve
0
jlevieCommented:
The short answer is no, a subshell can't modify the environment of it's parent. The way the environment works is that each child process (an executable, a shell) inherits it's environment from it's parent, the parent being being whatever process launched the child. The inheritance is one way and the parent is (thankfully) unaware of anything that the child does to the environment.

The way to get env variables set in the parent shell from a script is to treat the script as a series of shell commands executed in the current shell. You can do this by executing the script like:

$ . script-file [args]

The current PATH is used searched to find the directory containging "script-file" and any arguments are passed to the script. For mor information see the man pages for ksh.
0
ahoffmannCommented:
it's as s_franklin already described, you need to source the shell script
I only know of another, very cumbersome, solution which might work depending on what you want to aquire.
0
wittyAuthor Commented:
that was what I neede!!

thanks to all of you!!

witty
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.