Createing and updateing persistant environment variables
Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
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.
Createing and updateing persistant environment variables
Hi,
What I am trying to figure out here is how to create an environment variable that persists across multiple terminal prompts. I can do that (sort of) by adding them to ~/.bashrc file (and/or /etc/profile) but I'd like to be able to change these variables on the fly, open a new terminal and have the variable exist with the new value. Is that possible? I THOUGHT that was what export did but apparently It's not.
Heres an example of what I'm doing (or trying to do).
Environment variables that are 'exported' propagate down to child processes. Therefore, if you want to launch a new terminal, and have the desired environment variables exist, you will need to launch said terminal as a child of the shell that contains the environment variables. If you are using xterm as your terminal, you can do this by typing 'xterm' at the command prompt, and it will launch an xterm containing a shell with the desired variables. Same applies for other terminal emulators.
--- rod.
Createing and updateing persistant environment variables
Hi tehNbomr. Thanks for the response but I can't get that to work either.
I opened a prompt and checked $TERM... it is indeed xterm so i changed WIFIMAC and exported it like so...
Code:
export WIFIMAC=TESTING
then i typed xterm and it spit out the following
Code:
Warning: Cannot convert string "ni12" to type FontStruct
xterm: unable to open font "-Misc-Fixed-Medium--20-200-75-75-C-100-ISO10646-1", trying "fixed"....
It did open a new xterm (that looked completely different than the parent window) but it ties up the parent terminal window until its closed and when i tried to echo $WIFIMAC... it was nonexistant
Update: It is actually Konsole... but the problems are all the same.
Last edited by Consigliere; 06-25-2007 at 03:24 PM.
Your child terminal can be launched in background mode:
Code:
konsole &
# Note: ^
As to why your exported variables are not appearing in the child processes, I can only guess that either you have made an error in setting or testing for your variable. Or, there is some script buried in the chain of bash startup scripts that clears (unsets) all variables except some set that it likes. Tell us what flavor of Linux you are using, and perhaps someone familiar with it will tell us whether that distro has such a configuration.
--- rod.
but just the same, The variable problem persists. I am primarily trying to get this to work on Backtrack2. Everything works just like you explained on Ubuntu 7 though so maybe it is an issue with BT2?
and then echo $WIFIMAC in the new terminal... I get the ORIGINAL value that is set in the profile files. if I do the same with a new variable that DOESNT exist in those files like...
Environment variables that are 'exported' propagate down to child processes. Therefore, if you want to launch a new terminal, and have the desired environment variables exist, you will need to launch said terminal as a child of the shell that contains the environment variables. If you are using xterm as your terminal, you can do this by typing 'xterm' at the command prompt, and it will launch an xterm containing a shell with the desired variables. Same applies for other terminal emulators.
--- rod.
I mean that everything in the .bashrc will not be even read by bash for login shells. Environment variables or redirection or whatever from profiles will be kept yes.
Quote:
yeah But that just makes things more complicated
It's a nice feature, I used it for increasing my post count of something like 20 posts on LQ because nobody's aware of this
One more
nx5000
It's never been really clear to me what constitutes a 'login'. If I launch xterm from a bash commandline, it starts up an xterm with a bash shell running in it. The (new) bash shell is running as a child process of the xterm, which is a child process of the (original) bash shell in which I entered the 'xterm' command. What make the child bash process a 'login' (or not)?
I always lose track of what bash startup scripts execute & when, but is there not at least one script which is sourced each and every time bash starts? Is there a way to force bash to source one or more scripts on startup? If so, perhaps the original poster has some alias set which causes it to do so, invisibly. I really can't see very many ways that an arbitrary environment variable can be 'deleted'. Environments are not just a shell concept, but apply to all processes. As such, it seems strange that variables are disappearing.
It's not so clear for the users, that's why sometimes you have this in distro-default .profile:
Quote:
Originally Posted by .profile
# include .bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
I suggest you look at the man page of bash, the part INVOCATION.
There are so many exceptions that it would take me long to explain.
When you login to your machine, you get a login shell.
You can mimic this:
sudo login
enter user/pass
echo $0
-bash the - means login shell. Login shells will parse the profile files.
the next shells will be by default non login shells.
They will inherit from the login shells the ENV variables.
And they will source .bashrc
When you open an xterm from xwindow, you open a nonlogin shell.
If you want to keep track of whats happening, you can add a line like this:
echo "Sourcing bashrc" | logger
at the start of .bashrc
You will get the message in /var/log/messages
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.