LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-28-2004, 11:27 AM   #1
jayjoh
LQ Newbie
 
Registered: Jun 2004
Location: Fairborn,OH
Distribution: Red Hat
Posts: 4

Rep: Reputation: 0
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?
 
Old 06-28-2004, 12:24 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 06-29-2004, 08:20 AM   #3
jayjoh
LQ Newbie
 
Registered: Jun 2004
Location: Fairborn,OH
Distribution: Red Hat
Posts: 4

Original Poster
Rep: Reputation: 0
>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.)
 
Old 06-29-2004, 11:11 AM   #4
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
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
 
Old 06-29-2004, 11:19 AM   #5
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
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.
 
Old 06-30-2004, 09:32 AM   #6
jayjoh
LQ Newbie
 
Registered: Jun 2004
Location: Fairborn,OH
Distribution: Red Hat
Posts: 4

Original Poster
Rep: Reputation: 0
>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.
 
Old 06-30-2004, 09:51 AM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
 
Old 06-30-2004, 10:06 AM   #8
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215
Blog Entries: 2

Rep: Reputation: 49
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.
 
Old 06-30-2004, 10:11 AM   #9
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
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)
 
Old 07-12-2004, 12:35 PM   #10
jayjoh
LQ Newbie
 
Registered: Jun 2004
Location: Fairborn,OH
Distribution: Red Hat
Posts: 4

Original Poster
Rep: Reputation: 0
>(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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment variables setting... simjii SUSE / openSUSE 4 11-19-2005 07:50 PM
Setting Environment Variables rbh123 Linux - Newbie 2 10-03-2005 06:34 PM
Need help setting environment variables. cottonmouth Linux - Newbie 3 12-09-2004 04:54 PM
setting environment variables durden2.0 Linux - Newbie 5 07-15-2003 01:57 PM
setting environment variables rezza Linux - Distributions 2 04-09-2003 08:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration