LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   setting environment variables (http://www.linuxquestions.org/questions/linux-newbie-8/setting-environment-variables-198700/)

jayjoh 06-28-2004 12:27 PM

setting environment variables
 
This seems so elementary I'm almost embarassed to ask, but ...

Using Fedora, bash ...

If I set an environment variable with "export SOMETHING=whatever" from the shell prompt, it works fine. But if I try to set an environment variable the same way from within a shell script, once the script ends the variable is gone. I vaguely recalled from my old unix days that if I put a period in front of the script name -- as in ". myscript" instead of simply "myscript" -- that this did something useful in this regard and indeed if I do that in Linux then the environment variables "survive" the end of the shell script.

So my question is: Is there a way to set an environment variable in a shell script so that it will be set at the shell level and not just within the script, without doing the period business (which is annoying to document for others or even remember)?

Also, according to the man pages, "set" is supposed to set shell variables. But as far as I can see it does nothing. Can anyone clarify for me what it does?

Dark_Helmet 06-28-2004 01:24 PM

A shell script runs in a separate shell. To put it another way, when you run a script, the script executes in a completely distinct and separate shell from the one you started the script in.

So, if you're in shell A and execute a script, the script executes entirely within shell B. When the script finishes, shell B is destroyed. So, with that in mind, realize that your script is setting environment variables, but it's setting them in shell B.

Renaming a file with a dot ('.') at the beginning won't change the situation. You might be leading towards sourcing a file though. Bash provides a typing shortcut for the source command: a dot. When you source a file, it's as if you execute the statements in the file from within the shell you are in. To source a file with the shorthand, you would do something like: . some_file

Note that there's a space between the dot and the filename. :)

jayjoh 06-29-2004 09:20 AM

>When the script finishes, shell B is destroyed.

Yes, I understand that. But is there anything I can do about it? Is there any way to set an environment value "up the chain"? Or can I only set it for my own "shell level" and below? i.e. is there any way for a shell script to set environment variables for the parent? What I'm trying to do here is put some environment settings that I need into a shell script so when I want to work on this project, I execute the shell script, and, bang, they're all set.

>Renaming a file with a dot ('.') at the beginning won't change the situation. You might be leading towards sourcing a file though. Bash provides a typing shortcut for the source command: a dot. ... To source a file with the shorthand, you would do something like: . some_file ... Note that there's a space between the dot and the filename.

Yes, sorry, I didn't mean putting a dot as part of the file name, I meant putting it in front of the name like you describe. I guess it's not clear with the font being used here that there was a space. I rather figured that it must be executing the commands in the current shell rather than spinning a sub-shell like you describe. (Point for me! I guessed right.)

arobic 06-29-2004 12:11 PM

Hi!

why don't you put these environment variables definitions in one of your bash configuration file so that they are setup when you open a shell?

For example, you could define yourself a file called .userrc and execute it by adding the following line to .bashrc ou .bash_profile:
source .userrc

mikshaw 06-29-2004 12:19 PM

What I would likely do in this situation is open a terminal as a base for the project.
There would be a file containing
export SOMETHING0=whatever0
export SOMETHING1=whatever1
export SOMETHING2=whatever2
etc...

Source the file from the project terminal, and then launch the necessary applications from the same term.

jayjoh 06-30-2004 10:32 AM

>Source the file from the project terminal

What do you mean by that? If you mean run it with the "dot", that's what I'm doing at the moment, it just seems a bit awkward. Okay, sometimes Linux gets a bit awkward, maybe that's just how it is.

>why don't you put these environment variables definitions in one of your bash configuration file so that they are setup when you open a shell?

Yes, I could do that. I was thinking that I don't want to "junk up" my environment with a bunch of stuff that I'll only use for one project, and then a year from now I'll look at that profile file and not remember what it was for so I'm not sure whether or not I can take it out, etc.

Thanks all for the help. From the fact that no one's given me the solution I was hoping for -- some command that alters the environment "upstairs" -- I'm sadly concluding that maybe there is no such command. Bummer.

Dark_Helmet 06-30-2004 10:51 AM

No, I don't think you can pass environment variables to a parent process.

To address your concerns about your profile getting cluttered, you can simply add comments. Something like:
Code:

# Environment variables added to support project X
# Added: June 30, 2004
PROJECT_HOME=/path/to/some/dir
SOME_OTHER_VAR="some other string"

export PROJECT_HOME SOME_OTHER_VAR
# End of variables for project X

or, if you don't like that, create an entire file to encapsulate the changes and do something like:
Code:

# Include variables for project X.
# It is safe to remove the following if statement and the associated file
# when work on project X is complete.
if [ -e ~/.projectx_variables ] ; then
  . ~/.projectx_variables
fi


lugoteehalt 06-30-2004 11:06 AM

Quote:

Thanks all for the help. From the fact that no one's given me the solution I was hoping for -- some command that alters the environment "upstairs" -- I'm sadly concluding that maybe there is no such command. Bummer.
Have no idea at all if this works, but in .bash_profile you get stuff like:
export MANPATH="/filth/filth"
i.e. there are quotes, ", don't know if I just put them in right place.

Might that do it?? No reason why it should, but it might.:D

arobic 06-30-2004 11:11 AM

Quote:

Might that do it?? No reason why it should, but it might
No, Dark_Helmet was right. There is absolutely no way to export shell variables to parent process in bash

ref (From advanced bash-scripting guide, a very good reference by the way!):
http://www.tldp.org/LDP/abs/html/int...html#EXPORTREF (read first paragraph)

jayjoh 07-12-2004 01:35 PM

>(From advanced bash-scripting guide, a very good reference by the way!):

Ah well, at least I now have a reference saying that, yes indeed, what I want to do is impossible, so I can get on with my life. 'Better to know it can't be done and quit beating my head against a wall.


All times are GMT -5. The time now is 02:02 AM.