×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Setting ENV variables in command shell

Setting ENV variables in command shell

Setting ENV variables in command shell

(OP)
Hi,

I'm trying to read ENV varaibles from an ini file and set these variables in the cmd.exe.

Unfortunatly, when I set ENV variables, they are only set in the perl process shell and not in the parent shell.

Does anybody know how to resolve this?


Thierry

RE: Setting ENV variables in command shell

Well, the problem is when you run a perl script, it runs as a child process of the shell you call it from, so changes to the child don't take place in the parent.  The only way that I  would know of would be to actually add them to the .bash_profile or .mycshrc file, so they are automatically set, but that's kind of a permanant thing, and still doesen't work for your problem, because you have to restart the shell.

You may be able to fake it with an exec or system or backtick call to a shell with setenv...., but this may create yet another sub-shell.  You'll have to read the docs to see if one of these calls actually uses the shell you are running the script from.  I hope that makes sense, if not, let me know and I"ll explain further.

As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.

RE: Setting ENV variables in command shell

(OP)
This all makes sense....


Thanks for the shot.


Thierry

RE: Setting ENV variables in command shell

You could also try exporting the environment variables after you set them. This works on some *nix systems, not sure which. Export is an *nix command, so check the man pages for your system.

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.

RE: Setting ENV variables in command shell

No, this will still not work, please read my previous post, I believe it explains why.



Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.

RE: Setting ENV variables in command shell

Actually, it doesn't explain why. Isn't the purpose of the export command to export environment variables from a sub-shell to the main shell?

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.

RE: Setting ENV variables in command shell

Errrmmm no tsdragon, it isn't

when you say something like this in a script

export MYVAR

That means that when THAT SCRIPT starts a subshell the variable MYVAR will be visible to the subshell.

It does not mean that the program that called your script will able to see MYVAR.

You can't, as far as I know, do what the OP wanted. However.... You *can* do this:

$ENV{'var_one'} = 'hello';
$ENV{'var_two'} = 'goodbye';
system('some_command');

the program 'some_command' will then have access to the variables var_one and var_two.

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.

RE: Setting ENV variables in command shell

What I meant was to use the system command to set and export the environment variables. Use the semicolon, or whatever your flavor of *nix uses to separate commands and put the set commands and the export commands in the same command line. It's worth a try.

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.

RE: Setting ENV variables in command shell

Have a go ts, don't *think* that will work -- here's why I think so.

The system() function starts a subshell, a child process. This child process only last as long as the system() function lasts and, when it finishes, any variables it sets or changes will disappear unless it leaves child processes of its own.

I like your sig by the way <smile>

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.

RE: Setting ENV variables in command shell

Thanks, I borrowed it from somewhere, and changed catsup to mustard (I hate catsup).

I know system starts a child process (or subshell), but my understanding is that the export command is supposed to "export" the named environment variable(s) from the subshell to the "main" shell environment. Tomorrow morning I'll check the man page for export, and maybe even try it out from perl to see how it works.

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.

RE: Setting ENV variables in command shell

catsup..... sounds horrible, if I knew what it was I'm sure I'd hate it....

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.

RE: Setting ENV variables in command shell

On SunOS 5.8 -  man export gives,

"export ...... functions to determine the characteristics for the environmental variables of the current shell and its descendents."

BTW - Is it dangerous to stand behind a dragon?  Seems like it might be if he has had to much mustard? <yuk-yuk-yuk>  Or, is that how they fly????
.... Sorry, I'm a connoiseur of poor humor.


 
 
 keep the rudder amid ship and beware the odd typo

RE: Setting ENV variables in command shell

That description from the SunOS man page isn't real helpful, but since it says "descendents" it may not export to the "main" shell. I'll have to try it out and see what happens.

Now that I've post ON topic...

Catsup is an alternative spelling of ketchup. You know, that nasty tomato and water stuff that Ronald Reagan thought should qualify as a vegetable in school lunches.

It's only dangerous to stand behind a dragon if he has been eating hot mustard. Salsa, jalapenos and Tabasco sauce are even more dangerous. (In dragon talk we call those "jet fuel".)

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close