LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 06-25-2007, 01:10 PM   #1
Consigliere
LQ Newbie
 
Registered: Feb 2005
Posts: 16

Rep: Reputation: 0
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
Code:
echo $WIFIMAC
, 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
Code:
echo $WIFIMAC
... 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!
 
Old 06-25-2007, 02:08 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-25-2007, 03:20 PM   #3
Consigliere
LQ Newbie
 
Registered: Feb 2005
Posts: 16

Original Poster
Rep: Reputation: 0
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.
 
Old 06-25-2007, 04:05 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-25-2007, 04:42 PM   #5
Consigliere
LQ Newbie
 
Registered: Feb 2005
Posts: 16

Original Poster
Rep: Reputation: 0
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?
 
Old 06-25-2007, 04:51 PM   #6
Consigliere
LQ Newbie
 
Registered: Feb 2005
Posts: 16

Original Poster
Rep: Reputation: 0
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
 
Old 06-25-2007, 05:23 PM   #7
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
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"
 
Old 06-25-2007, 05:59 PM   #8
Consigliere
LQ Newbie
 
Registered: Feb 2005
Posts: 16

Original Poster
Rep: Reputation: 0
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
 
Old 06-25-2007, 06:47 PM   #9
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
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
 
Old 06-25-2007, 07:30 PM   #10
Consigliere
LQ Newbie
 
Registered: Feb 2005
Posts: 16

Original Poster
Rep: Reputation: 0
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
 
Old 06-25-2007, 07:45 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 06-26-2007, 11:10 AM   #12
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
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
 
Old 06-26-2007, 02:12 PM   #13
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
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.
 
  


Reply



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 xeon123 Linux - General 6 05-18-2007 07:57 PM
environment variables nasht Linux - General 3 08-11-2005 02:12 PM
environment variables user_linux Linux - Newbie 3 06-15-2005 08:46 AM
environment variables Prakhardeep Linux - Software 3 10-06-2004 03:30 AM
environment variables aethereal Linux - Newbie 7 12-19-2001 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

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

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