Setting an Environment Variable from a shell script

I have been given a good reason why this is impossible but I seem to remmber that I have seen it done some where.  If you know how I can do this let me know otherwise just insult me and be done.

esentially I want to setenv JUNK /ORANGE/Dogs run the script and then echo $JUNK and see the path on the command line.  shell script doesn't matter.

Have tried borne, korn, perl, tcsh, csh.

If I am not missing something can I do what I want with a C or Java executable
SafeSqlAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jlevieConnect With a Mentor Commented:
I'm not aware of any way to do what you want other than the ". cmd" or "source cmd" method. As far as I know the same restriction that applies to executing a shell script applies to a perl, C, Java, etc. program. In each case the process that is fiddling with the environment is a sub process of the current shell and thus the current shell won't see any changes the subprocess makes in its environment.
0
 
jlevieCommented:
My understanding of what you want is to set an env variable from with a script and have that variable visible in the current shell after executing the script.

That's eaily enough done if the script is correctly executed. For instance if I had a file named gork containing:

spork="dowha diddy"
export spork

I could do:

chaos> . gork
chaos> echo $spork
dowha diddy

In a like manner I could modify the PATH, i.e.,

chaos> cat >gork <<EOF
> PATH=$PATH:$JUNK
; export PATH
> EOF
chaos> JUNK=/ORANGE/Dogs; export JUNK
chaos> . gork
chaos> echo $PATH
/usr/local/bin:/usr.../bin:/ORANGE/Dogs

You could also do this with a C  or program. The important fact to remember is that for the change in the environment to persist after the script exits a sub shell can't be used to execute whatever will be changing the environment. And that's becahuse inheritance is downwards into subshells but not upwards into the parent.
0
 
ahoffmannCommented:
more specific using jlevie's gork example:

sh# . ./gork
ksh# . ./gork
bash# . ./gork

for csh and tcsh gork must look like:

setenv spork "dowha diddy"

then use it like:

csh% source ./gork
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
chris_calabreseCommented:
Just as an aside, it is possible to do this in Plan 9 through /proc.  I don't remember the exact syntax, but it'd be something akin to this:

echo /ORANGE/Dogs > /proc/$PPID/env/JUNK
0
 
SafeSqlAuthor Commented:
Both answers work which is good. Problem is that I am trying to get a "command" that I can give a less than unix savy person so that they can setup a test environment.

For instance

/home/smith>echo $target_system
production
/home/smith>go_developement
YOU ARE NOW SAFE TO RUN
/home/smith>echo $target_system
development

Thus I can maintain the settings by modifing the go_development file.  The gork solution works but I worry the user will forget the ". " or the "source"

As I understand it if go_development is a "shell script" this is not possible because inheritance.  ie. the shell script makes its own subprocess based on the parent.

So I guess the question is would a c or java executable create its own "shell" or does it run in the parent shell? and thus...
0
 
ahoffmannCommented:
assuming go_developement is a shell script, and switch_env is the shell script to be sourced to switch environment, you have following choices:

# csh, tcsh:
# go_developement contains following:
source switch_env

# sh, ksh, bash:
# go_developement contains following:
. switch_env


alternatively you may provide shell aliases instead, like

# csh, tcsh:
alias dev     'source switch_env; go_developement'

# ksh, bash:
alias='. switch_env; go_developement'
0
 
SafeSqlAuthor Commented:
Both answers work which is good. Problem is that I am trying to get a "command" that I can give a less than unix savy person so that they can setup a test environment.

For instance

/home/smith>echo $target_system
production
/home/smith>go_developement
YOU ARE NOW SAFE TO RUN
/home/smith>echo $target_system
development

Thus I can maintain the settings by modifing the go_development file.  The gork solution works but I worry the user will forget the ". " or the "source"

As I understand it if go_development is a "shell script" this is not possible because inheritance.  ie. the shell script makes its own subprocess based on the parent.

So I guess the question is would a c or java executable create its own "shell" or does it run in the parent shell? and thus...
0
 
SafeSqlAuthor Commented:
Thanks for the help
0
 
ahoffmannCommented:
is the graded answer realy the one which solved your problem?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.