Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.)
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.
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.
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
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.
>(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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.