| Linux - Desktop This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-25-2007, 01:10 PM
|
#1
|
|
LQ Newbie
Registered: Feb 2005
Posts: 16
Rep:
|
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).
This command will give me the mac address of ath0
Code:
ifconfig ath0 | grep HWadd | cut -b39-55
So i have this in /etc/profile and ~/.bashrc
Code:
export WIFIMAC=`ifconfig ath0 | grep HWadd | cut -b39-55`
(I have many wifi cards or i would just hard code the mac)
So... if I open a terminal after logging in... and , it prints my mac address to the screen.
Now if run the following command... (notice it is ath1 now)
Code:
export WIFIMAC=`ifconfig ath1 | grep HWadd | cut -b39-55`
The value of WIFIMAC is now ath1's MAC address... so far so good.
BUT... if i open a new terminal window and ... the value is that of ath0 because that is what is in /etc/profile and/or ~/.bashrc
hopefully this all makes some kind of sense :-)
any insight here would be greatly appreciated.
Thanks!
|
|
|
|
06-25-2007, 02:08 PM
|
#2
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,321
|
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.
|
|
|
|
06-25-2007, 03:20 PM
|
#3
|
|
LQ Newbie
Registered: Feb 2005
Posts: 16
Original Poster
Rep:
|
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.
|
|
|
|
06-25-2007, 04:05 PM
|
#4
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,321
|
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.
|
|
|
|
06-25-2007, 04:42 PM
|
#5
|
|
LQ Newbie
Registered: Feb 2005
Posts: 16
Original Poster
Rep:
|
ahhh konsole& i KNEW that too lol
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? 
|
|
|
|
06-25-2007, 04:51 PM
|
#6
|
|
LQ Newbie
Registered: Feb 2005
Posts: 16
Original Poster
Rep:
|
OK, so apparently the problem is with the variables being set in ~/.bashrc and/or /etc/profile
The variables are reset to the original value from one of these files every time a new terminal is opened (regardless of where or how it was opened)
So i have WIFIMAC set in /etc/profile and ~/.bashrc and if I open a terminal and type...
Code:
bt ~ # WIFIMAC=TESTING
bt ~ # export WIFIMAC
bt ~ # konsole&
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...
Code:
bt ~ # TEST=TESTING
bt ~ # export TEST
bt ~ # konsole&
then echo $TEST in the new terminal... it persists and displays "TESTING"
I guess it makes sense... I just won't be putting the variables in the .bashrc or /etc/profile
Thanks for helping to set me straight on what exactly export does. You've been a great help.
Cheers
|
|
|
|
06-25-2007, 05:23 PM
|
#7
|
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
Quote:
|
Originally Posted by theNbomr
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.
|
+
bashrc is not source by login shell
eg:
su -
will not source /root/.bashrc
"It's a feature"
|
|
|
|
06-25-2007, 05:59 PM
|
#8
|
|
LQ Newbie
Registered: Feb 2005
Posts: 16
Original Poster
Rep:
|
Quote:
bashrc is not source by login shell
eg:
su -
will not source /root/.bashrc
|
You mean it won't include the users environment? yeah But that just makes things more complicated 
|
|
|
|
06-25-2007, 06:47 PM
|
#9
|
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
Quote:
|
Originally Posted by Consigliere
You mean it won't include the users environment?
|
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
|
|
|
|
06-25-2007, 07:30 PM
|
#10
|
|
LQ Newbie
Registered: Feb 2005
Posts: 16
Original Poster
Rep:
|
I guess I'm ahead of the game then cause I knew exactly what 'su -' was
Good luck with your quest to post more heh
|
|
|
|
06-25-2007, 07:45 PM
|
#11
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,321
|
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.
--- rod.
PS Consigliere: What is Backtrack2?
Last edited by theNbomr; 06-25-2007 at 07:48 PM.
|
|
|
|
06-26-2007, 11:10 AM
|
#12
|
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
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
|
|
|
|
06-26-2007, 02:12 PM
|
#13
|
|
Member
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102
Rep:
|
Why not just do
Code:
if [[ -z "$WIFIMAC" ]]; then
export WIFIMAC=`ifconfig ath0 | grep HWadd | cut -b39-55`
fi
in ~/.bashrc to set WIFIMAC only if it isn't set already. Thus, resetting it on the command line will persist into subshells.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:01 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|